Arcane Tech Stuff - Working with Large CSVs in SQLite

Questions, feedback, suggestions? Check out the comments

I ran into a situation recently where a CSV file was just too large to import into Google Sheets (my traditional work-zone for most data analysis projects). After going down the rabbit hole, I realized that not only is there a great way around this, but also a better work-zone on offer for anyone with SQL skills.1

Full credit to Eliud Rodriguez and this Medium post for walking through the setup here — refer to that post if you want a more detailed tutorial on how to set things up for yourself.

The Question

How do I run analysis on CSV files that are too large to import into Google Sheets? And is there a better way to actually run that analysis?

The Answer

Using a short Python script, pandas, and sqlite3, you can create a localized database and load any CSV file into it as new table.2 Then you can open that database with DB Browser for SQLite and analyze all of your data using the full power of SQL. This includes the ability to run JOINs and pull together data across your CSVs into new tables, and the option to export those new tables as brand-new CSV files.

Not only does this let you open extremely large CSV files, it also unlocks a far broader set of functionality than what’s available within Google Sheets or Excel.3

Context

I introed this as a solution to the problem of large CSVs, but this approach is also a tremendous unlock for a lot more than just the analysis of large datasets. If you don’t have a data warehouse in place, working with data across systems can be a bit of a nightmare and working with it inside of a spreadsheet is often the only option. But even then, linking up datasets relies on VLOOKUP and INDEX(MATCH()) formulas, which eventually slow your sheets to a crawl. There’s tricks and workarounds here too (wrapping a VLOOKUP inside an ARRAYFORMULA within Google Sheets can save you some processing time), but it’s nothing compared to the power of a simple LEFT JOIN.

Running analysis locally also gives you huge speed boosts, means you’re not costing your company compute resources, and additionally allows you to experiment without worrying about breaking anything important.

Doing it yourself

I’m going to gloss over the process of getting Python up and running here. It’s a bigger topic and there’s better resources out there. Just be sure to install pandas and sqlite3 so you can use them in your program like so:

pip3 install pandas
pip3 install sqlite3

Then you can run this Python script any time you want to add a CSV to an existing database, or create a new one:

import pandas as pd
import sqlite3

database = input('Database Name: ')
csvFile = input('CSV File Name: ')
df = pd.read_csv(csvFile)
conn = sqlite3.connect(database+'.db')
cur = conn.cursor()
tableName = str(input('Table Name: ')).lower()
df.to_sql(tableName,conn,if_exists='replace', index=False)
print(pd.read_sql('''SELECT * FROM '''+tableName, conn))

Then you can use DB Browser for SQLite to open your new .db file and start analyzing your table(s) with SQL.

Best of luck out there with wherever your analysis takes you!


  1. No guarantees here — we’re in the realm of hacky code solutions and this might not be the best solution for your specific situation. But it worked for me and it might help you out one day!↩︎

  2. You could also definitely use pandas for this on its own, as it’s a great tool for data analysis. I, however, know SQL and I don’t know pandas. If you do, this is probably way too basic for you anyways :)↩︎

  3. This is also a fantastic way to learn SQL with real data. Take a few Salesforce report exports, import them into a new database, and start experimenting from there↩︎


Written by Jack Segal. Shoot me an email with questions or comments, and if you'd like to support my writing, you can send me a tip using Stripe!



Date
2024-06-25



Subscribe

Fill out the form to get notified whenever a new post goes live!

Powered by Buttondown.



Comments