Python — Pandas
Install & the two core types
pip install pandas
import pandas as pd
# Series — one labeled column
s = pd.Series([90, 85, 77], name="score")
# DataFrame — a full table (the star of the show)
df = pd.DataFrame({
"name": ["Ada", "Bo", "Cleo"],
"age": [36, 22, 45],
"score": [91, 78, 88],
})
name age score
0 Ada 36 91 ← the numbers on the left are the index
1 Bo 22 78
2 Cleo 45 88
Loading real data
df = pd.read_csv("users.csv")
df = pd.read_excel("sales.xlsx") # needs openpyxl installed
df = pd.read_json("data.json")
df.head() # first 5 rows — your first command every time
df.shape # (rows, columns)
df.info() # dtypes + missing-value overview
df.describe() # instant statistics summary
Selecting columns & rows
df["name"] # one column → Series
df[["name", "score"]] # multiple → DataFrame
df.loc[2] # row by LABEL
df.iloc[0] # row by POSITION
df.at[1, "age"] # single cell: label row, column name
Filtering — boolean masks
df[df["score"] > 80] # passing students
df[(df["age"] < 30) & (df["score"] > 80)] # AND — parentheses required!
df[(df["role"] == "admin") | df["active"]]
Adding & changing columns
df["passed"] = df["score"] >= 80 # computed column
df["score_adj"] = df["score"] + 5 # vectorized like NumPy
df = df.rename(columns={"name": "full_name"})
df = df.drop("temp", axis=1) # remove column
Grouping & aggregation
df.groupby("department")["salary"].mean()
df.groupby("dept").agg(
avg=("salary", "mean"),
count=("salary", "count"),
)
df.sort_values("score", ascending=False)
df["score"].value_counts()
Handling missing data
df.isna().sum() # how many NaNs per column
df.dropna() # drop incomplete rows
df.fillna(0) # fill with value
df["age"].fillna(df["age"].median()) # smarter fill
The typical analysis in 6 lines
import pandas as pd
df = pd.read_csv("sales.csv")
monthly = df.groupby("month")["revenue"].sum()
best = monthly.idxmax()
print(f"Best month: {best} (${monthly.max():,.0f})")
Gotchas: chained indexing (
df[a][b] = x) silently fails to write — use.loc;&/|notand/orinside filters; SettingWithCopyWarning means operate on.copy().
Mini Practice
- Build a 5-row DataFrame by hand; run head/describe.
- Filter rows by two conditions with & .
- Add a derived column from arithmetic on others.
- groupby category → mean of values; sort result.
- Load any CSV; report shape, missing counts, top row.
Next: matplotlib →
Related Topics
Frequently Asked Questions about Pandas
What is Pandas in Python?
Pandas is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Pandas?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Pandas.
Why is Pandas important in Python?
Pandas is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.