Converting CSV to a SQLite Database
As a part of my data science course on EdX we have been working with a lot of csv files. I spoke SQL long before I spoke Pandas and I find that it is much easier to do initial exploration of the data using raw SQL queries compared to the Pandas DSL.
Kaggle is a great repository full of useful data sets that are ripe for exploration. While a lot of these data sets come in both csv and sql flavors, some of them are CSV only. Using SQLit we are able to easily import these csv files into a database and then run queries for further data exploration.
Im going to use the kickstarter data set for this tutorial, feel free to download the csv files from kaggle so that you can follow along.
Pre Requisites
Make sure that you have SQLite installed before getting started with this tutorial.
Steps to Convert CSV to SQLite
First, Download the data set from kaggle, this will come in the form of a zip file. Unzip this and open up a terminal in the directory where you have the new unzipped kickstarter-projects folder.
In your terminal open up a new sqlite session followed by the name of the file that you want to save your new database to.
sqlite3 ks.db
Inside of the sqlite shell, change the mode to csv.
.mode csv
Import the csv file, and add the name of the table that you want the data to be imported into.
.import kickstarter-projects/ks-projects-201801.csv ks
Verify that everything was imported correctly. Take a look at the schema, and first couple of rows. Your output should look something like this:
sqlite> .schema ks
CREATE TABLE ks(
"ID" TEXT,
"name" TEXT,
"category" TEXT,
"main_category" TEXT,
"currency" TEXT,
"deadline" TEXT,
"goal" TEXT,
"launched" TEXT,
"pledged" TEXT,
"state" TEXT,
"backers" TEXT,
"country" TEXT,
"usd pledged" TEXT,
"usd_pledged_real" TEXT,
"usd_goal_real" TEXT
);
sqlite> select * from ks limit 5;
1000002330|The Songs of Adelaide & Abullah|Poetry|Publishing|GBP|2015-10-09|1000.00|2015-08-11 12:12:28|0.00|failed|0|GB|0.00|0.00|1533.95
1000003930|Greeting From Earth: ZGAC Arts Capsule For ET|Narrative Film|Film & Video|USD|2017-11-01|30000.00|2017-09-02 04:43:57|2421.00|failed|15|US|100.00|2421.00|30000.00
1000004038|Where is Hank?|Narrative Film|Film & Video|USD|2013-02-26|45000.00|2013-01-12 00:20:50|220.00|failed|3|US|220.00|220.00|45000.00
1000007540|ToshiCapital Rekordz Needs Help to Complete Album|Music|Music|USD|2012-04-16|5000.00|2012-03-17 03:24:11|1.00|failed|1|US|1.00|1.00|5000.00
1000011046|Community Film Project: The Art of Neighborhood Filmmaking|Film & Video|Film & Video|USD|2015-08-29|19500.00|2015-07-04 08:35:03|1283.00|canceled|14|US|1283.00|1283.00|19500.00
Excellent! Now you can query this entire data set as your normally would. Happy data exploration!
Thank you for reading! Share your thoughts with me on bluesky, mastodon, or via email.
Check out some more stuff to read down below.
Most popular posts this month
- Setting up ANTLR4 on Windows
- Meritocracy?
- Possible Plagiarism Made me Cringe
- SQLite DB Migrations with PRAGMA user_version
- Using Plex with Nextcloud
Recent Favorite Blog Posts
This is a collection of the last 8 posts that I bookmarked.
- The Rise of Bluesky from Communications of the ACM
- Podcaster, DJ, and writer DJ Louie XIV on going for it (even if you’re terrified) from The Creative Independent
- Useful Bluesky Tools from Robb Knight • Posts • Atom Feed
- Re: Bluesky from Colin Devroe
- From the Red Hell to the Sky of Blue from Straphanger
- We don’t need to use what we make from Derek Sivers blog
- Ubuntu Summit 2024: A joyful experience filled with sorrow from Planet KDE | English
- Sabotage from jwz
Articles from blogs I follow around the net
13/12/2024
# I can't believe it's been over a month since the last post. Not very good for someone who's supposed to be back. Still, I've also been struggling to get back to making music again; I've had a couple of ideas and put them on SoundClou…
via Colin Walker - Daily Feed December 18, 2024Christmas with Grubbs – an animated holiday special I worked on that you and your kids will love.
I am part of an animated holiday special and you can watch it on YouTube for free! Christmas With Grubbs is an animated holiday special based on Max Weaver's comic. It's about a little boy and his imaginary friend who get into all sorts of mischie…
via WIL WHEATON dot NET December 17, 2024Businessweek: Crypto Got What It Wanted in November’s Election. Now What?
The industry is now in a position to weaken financial rules that might protect consumers from the next crypto crash.
via Citation Needed December 17, 2024Generated by openring