Reading CSV, Excel & JSON Files
The first step in every real project is reading data. Knowing the right parameters for read_csv() and read_excel() saves hours of debugging on the first day of any data job.
How do you read a CSV file with Pandas?
import pandas as pd
df = pd.read_csv("sales.csv")
# Common parameters:
df = pd.read_csv("sales.csv",
sep=",", # delimiter (use "\t" for TSV)
header=0, # row to use as column names
skiprows=2, # skip first 2 rows
nrows=1000, # read only 1000 rows
usecols=["name","sales"], # only these columns
encoding="utf-8", # or "latin-1" for Indian data
na_values=["N/A", "-", "NULL"] # treat as NaN
)encoding="latin-1" (or "cp1252") is the most common fix for Indian data files exported from Excel. na_values lets you define what counts as missing beyond the default NaN.
How do you read an Excel file?
df = pd.read_excel("report.xlsx")
# Specific sheet:
df = pd.read_excel("report.xlsx", sheet_name="Sales")
# Multiple sheets → dict of DataFrames:
sheets = pd.read_excel("report.xlsx", sheet_name=None)
df_sales = sheets["Sales"]
df_hr = sheets["HR"]
# Skip rows and specific columns:
df = pd.read_excel("report.xlsx", skiprows=3, usecols="A:E")sheet_name=None reads all sheets at once. This is useful when you receive monthly reports where each month is a separate sheet and you need to combine them.
How do you read JSON data with Pandas?
# From file:
df = pd.read_json("data.json")
# From API response (list of dicts):
import json, requests
response = requests.get("https://api.example.com/data")
data = response.json() # list of dicts
df = pd.DataFrame(data)
# Nested JSON — normalize:
from pandas import json_normalize
df = json_normalize(data, record_path="items")json_normalize() is essential for nested JSON — the kind you get from REST APIs where each record has sub-objects. Without it, nested fields come in as dictionaries inside cells.
How do you write a DataFrame to CSV or Excel?
# CSV:
df.to_csv("output.csv", index=False) # index=False prevents saving row numbers
# Excel:
df.to_excel("output.xlsx", index=False, sheet_name="Results")
# Multiple sheets:
with pd.ExcelWriter("report.xlsx") as writer:
df_sales.to_excel(writer, sheet_name="Sales", index=False)
df_hr.to_excel(writer, sheet_name="HR", index=False)Always use index=False unless the index contains meaningful data. ExcelWriter is the correct way to write multiple sheets — do not call to_excel() on the same file twice.
How do you read a large CSV file efficiently?
# Read in chunks:
for chunk in pd.read_csv("large.csv", chunksize=10000):
process(chunk) # process each 10k row chunk
# Only load needed columns:
df = pd.read_csv("large.csv", usecols=["id","sales","date"])
# Reduce memory with dtype specification:
df = pd.read_csv("large.csv", dtype={"id": "int32", "sales": "float32"})For files larger than 1GB, chunking is necessary. Specifying dtypes upfront can reduce memory usage by 50-75% — int32 vs int64, float32 vs float64, and category for repeated strings.
EVIKA ACADEMY · PYTHON FOR DATA ANALYTICS
Want to master Python with live practice?
Join our Python for Data Analysis course — live classes in Noida and online across India.
Book Free Demo Class →