pandas · Lesson 1.3
Reading CSV files
The read_csv options you'll use most: dates, chosen columns, and values that mean "missing".
Practice data: PayPal files
Made up for teaching, not real PayPal data. Read a file with pd.read_csv("accounts.csv"). Disclaimer
accounts.csv (300 rows): account_id, created_at, country, us_state, account_type
fx_rates.csv (260 rows): rate_date, currency, usd_rate
merchants.csv (60 rows): merchant_id, merchant_name, category, country
raw_payouts.csv (120 rows): payout_id, merchant_id, amount_text, paid_on
refunds.csv (179 rows): refund_id, txn_id, refund_date, amount
transactions.csv (2,980 rows): txn_id, account_id, merchant_id, txn_ts, amount, currency, status, risk_score, us_state
wallet_entries.csv (1,375 rows): entry_id, account_id, entry_date, amount
1Dates come in as text
read_csv reads date columns as plain text (str). parse_dates=["col"] turns them into real datetimes you can do date maths with.
import pandas as pd
txns = pd.read_csv("transactions.csv", parse_dates=["txn_ts"])
print(txns["txn_ts"].dtype)
print(txns["txn_ts"].min(), txns["txn_ts"].max())Practice
Read transactions.csv with txn_ts as a datetime and store the number of days between the first and the last transaction in result.
import pandas as pd
txns = pd.read_csv("transactions.csv")
result = ...2Only the columns you need
usecols=[...] reads just some columns, which keeps big files fast and small.
import pandas as pd
small = pd.read_csv("transactions.csv", usecols=["txn_id", "amount", "currency"])
small.head()Practice
Read only the currency and amount columns of transactions.csv, and store the DataFrame in result.
import pandas as pd result = ...
3Values that mean "missing"
pandas already treats empty cells and text like N/A as missing (NaN). na_values=[...] adds your own placeholders, like pending.
import pandas as pd
raw = pd.read_csv("raw_payouts.csv")
print(raw["amount_text"].isna().sum())
clean = pd.read_csv("raw_payouts.csv", na_values=["pending"])
print(clean["amount_text"].isna().sum())Practice
Read raw_payouts.csv so that both N/A and pending count as missing, and store the number of missing amount_text values in result.
import pandas as pd
payouts = pd.read_csv("raw_payouts.csv")
result = ...4Peek at a big file
nrows=n reads just the first n rows, a quick way to look at a file before loading all of it.
import pandas as pd
preview = pd.read_csv("transactions.csv", nrows=3)
previewPractice
Read the first 10 rows of accounts.csv and store them in result.
import pandas as pd result = ...
Tip
In real projects files usually come from a path like pd.read_csv("data/2026/orders.csv") or a URL. The options are the same.