By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
A hyper-practical, zero-fluff guide for real projects and certifications (PL-300, DP-100, etc.)
You’re a data scientist or analyst. Your job isn’t just to build models—it’s to move data efficiently. Whether you’re: - Ingesting a 50GB CSV from a client, - Cleaning messy Excel files from business teams, - Storing processed data in a format that Spark can read in milliseconds, - Sharing results with stakeholders in JSON or Excel,
…you will use Pandas for I/O. If you don’t master this, you’ll waste hours debugging encoding errors, memory crashes, or slow reads/writes.
Real-world scenario:You inherit a legacy ETL pipeline that reads 100+ CSV files, merges them, and writes to Excel. The script runs for 2 hours and crashes. Your boss says, "Fix it by EOD." This guide is your lifeline.
pd.read_csv()
df.to_csv()
chunksize
pd.read_json()
df.to_json()
json_normalize()
pydantic
pd.read_excel()
df.to_excel()
.xlsx
.xls
openpyxl
xlrd
pd.read_parquet()
df.to_parquet()
pyarrow
fastparquet
chunksize=100_000
dtype
df['id'] = df['id'].astype('int32')
orient
'records'
'split'
'index'
engine
compression
'gzip'
'bz2'
'zip'
df.to_csv('data.csv.gz', compression='gzip')
index_col
index_col=False
pip install pandas pyarrow openpyxl
df = pd.DataFrame(np.random.rand(10_000_000, 5), columns=list('ABCDE'))
import pandas as pd # Read in chunks to avoid memory crash chunk_iter = pd.read_csv( "large_data.csv", chunksize=100_000, # Process 100K rows at a time dtype={"id": "int32", "value": "float32"}, # Save memory parse_dates=["timestamp"], # Auto-convert to datetime na_values=["NA", "NULL", ""] # Standardize missing values ) # Process each chunk for i, chunk in enumerate(chunk_iter): print(f"Processing chunk {i}...") # Do something with chunk (e.g., clean, filter) chunk = chunk.dropna()
Why this works:- chunksize prevents memory crashes.- dtype reduces memory usage.- na_values standardizes missing data.
na_values
# After processing all chunks, combine into one DataFrame df = pd.concat([chunk for chunk in chunk_iter]) # Write to Parquet (fast, compressed, columnar) df.to_parquet( "processed_data.parquet", engine="pyarrow", # Faster than fastparquet compression="snappy", # Good balance of speed/size index=False # Don't write index (saves space) )
Verify:
ls -lh processed_data.parquet # Check file size (should be ~10-20% of CSV)
Production insight:- Parquet is 10x faster to read than CSV in Spark/BigQuery.- snappy compression is faster than gzip (but slightly larger).
snappy
gzip
# Write to Excel (with multiple sheets) with pd.ExcelWriter( "report.xlsx", engine="openpyxl", # Required for .xlsx mode="w" # Overwrite if exists ) as writer: df.head(1000).to_excel(writer, sheet_name="Summary", index=False) # First 1K rows df.describe().to_excel(writer, sheet_name="Stats") # Summary stats
open report.xlsx # Check sheets
Production insight:- Excel is not for big data (max ~1M rows).- openpyxl is slower than xlsxwriter but supports more features.
xlsxwriter
# Simulate API response api_data = [ {"id": 1, "name": "Alice", "scores": [85, 90]}, {"id": 2, "name": "Bob", "scores": [75, 80]} ] # Write to JSON pd.DataFrame(api_data).to_json( "api_data.json", orient="records", # List of dicts (common for APIs) date_format="iso" # ISO 8601 timestamps ) # Read back df_json = pd.read_json("api_data.json", orient="records") print(df_json)
Output:
id name scores 0 1 Alice [85, 90] 1 2 Bob [75, 80]
Production insight:- orient="records" is most common for APIs.- Use json_normalize() to flatten nested JSON.
orient="records"
df.dtypes
read_csv()
compression="gzip"
index=False
.xlsm
to_csv()
na_values=["NA", "NULL", ""]
Trap: CSV is not scalable.
"How do you read a 10GB CSV without crashing?"
pd.read_csv(..., chunksize=100_000)
Trap: df = pd.read_csv("big.csv") will crash.
df = pd.read_csv("big.csv")
"What’s the fastest way to write a DataFrame to disk?"
df.to_parquet(..., engine="pyarrow")
Trap: Excel is slow and limited to ~1M rows.
"How do you handle nested JSON?"
pd.json_normalize()
Challenge:You have a 500MB CSV with 10M rows. Your script crashes when reading it. Fix it using chunksize and dtype, then write the cleaned data to Parquet.
Solution:
import pandas as pd # Read in chunks chunk_iter = pd.read_csv( "big_data.csv", chunksize=500_000, dtype={"id": "int32", "value": "float32"} ) # Process and combine df = pd.concat([chunk.dropna() for chunk in chunk_iter]) # Write to Parquet df.to_parquet("clean_data.parquet", engine="pyarrow")
Why it works:- chunksize prevents memory crashes.- dtype reduces memory usage.- Parquet is faster and smaller than CSV.
pd.read_csv("file.csv", chunksize=100_000, dtype={"col": "int32"})
df.to_csv("file.csv", index=False, compression="gzip")
pd.read_json("file.json", orient="records")
df.to_json("file.json", orient="records", date_format="iso")
pd.read_excel("file.xlsx", engine="openpyxl")
df.to_excel("file.xlsx", engine="xlsxwriter", index=False)
pd.read_parquet("file.parquet", engine="pyarrow")
df.to_parquet("file.parquet", engine="pyarrow", compression="snappy")
pd.json_normalize(data, "nested_key")
c
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.