Reading Excel, JSON & SQL
CSV files are great, but they're not the only game in town. Let me show you how to read other formats — because let's be honest, not everything lives in a CSV.
Reading Excel Files
Yes, you can read Excel files directly with Pandas. No more copy-pasting from spreadsheets:
import pandas as pd
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
print(df.head())
You might need to install openpyxl first: `pip install openpyxl`. The `sheet_name` parameter lets you pick which sheet to read. You can even pass a list of sheet names to read multiple sheets at once.
Reading JSON
JSON is everywhere — APIs, config files, web data. Pandas handles it beautifully:
df = pd.read_json('data.json')
print(df.head())
Reading from SQL
If you work with databases, this one's for you:
import sqlite3
conn = sqlite3.connect('database.db')
df = pd.read_sql('SELECT * FROM users', conn)
print(df.head())
Think of it like this — Pandas is a universal translator for data. CSV, Excel, JSON, SQL — it reads them all and gives you the same familiar DataFrame to work with.
Try it Yourself →Key Takeaways
- `pd.read_excel()` reads Excel files (needs openpyxl installed)
- `pd.read_json()` reads JSON files directly
- `pd.read_sql()` reads from SQL databases using a connection
- All these methods return the same DataFrame format