Pandas Quick Reference
1. Import Pandas
import pandas as pd
2. Reading Data
Load CSV files into a DataFrame:
df = pd.read_csv("data.csv") # read file
df = pd.read_csv("data.csv", index_col=0) # use first column as index
3. Inspecting Data
- Check dimensions:
df.shape # (rows, columns) - View top/bottom rows:
df.head() # first 5 rows df.tail() # last 5 rows - Column names:
df.columns - Quick summary:
df.info() # types, non-null counts df.describe() # summary statistics for numeric columns
4. Data Types (dtypes)
Check the type of a column or all columns:
df["price"].dtype # single column df.dtypes # all columns
Convert types:
df["points"].astype("float64")
5. Index
df.index.dtype df.index
6. Missing Data (NaN)
Detect missing values:
pd.isnull(df["country"]) # True if NaN pd.notnull(df["country"]) # True if not NaN
Fill or replace missing values:
df["region_2"].fillna("Unknown") # replace NaN
df["region_2"].fillna(method="bfill") # backfill
df["twitter_handle"].replace("@old","@new")
7. Core Objects
DataFrame: table of rows & columns
pd.DataFrame({"Yes":[50,21], "No":[131,2]})
Series: single column or list of values
pd.Series([1,2,3,4], index=["A","B","C","D"], name="Numbers")
Summary
Workflow: Read & inspect → check dtypes → handle NaN → manipulate/convert → analyze.