Python Pandas
pandas turns tables of data into DataFrames that you can filter, sort, group and summarise in a few lines. It reads CSV, Excel, JSON and SQL, which makes it the everyday tool for data analysis and for preparing test data.
- Create Series and DataFrames
- Read CSV files and inspect the data
- Select, filter and sort rows
- Handle missing values, group and merge
Install and import
python -m pip install pandasimport pandas as pdprint(int(pd.__version__.split(".")[0]) >= 2)True
The examples were run with pandas 3. The in-browser Try it uses an older pandas, so a few outputs (such as column types) may print slightly differently there.
Series and DataFrame
A Series is one labelled column. A DataFrame is a table of Series that share an index.
import pandas as pd scores = pd.Series([91, 78, 85], index=["Asha", "Ravi", "Meena"])print(scores)print(scores["Ravi"], scores.mean().round(2))Asha 91 Ravi 78 Meena 85 dtype: int64 78 84.67
import pandas as pd df = pd.DataFrame({ "name": ["Asha", "Ravi", "Meena", "John", "Sara"], "dept": ["QA", "Dev", "QA", "Ops", "Dev"], "salary": [52000, 61000, 48000, 55000, 67000], "years": [4, 6, 2, 5, 8],})print(df)print(df.shape)name dept salary years 0 Asha QA 52000 4 1 Ravi Dev 61000 6 2 Meena QA 48000 2 3 John Ops 55000 5 4 Sara Dev 67000 8 (5, 4)
Read a CSV file
import pandas as pd with open("orders.csv", "w") as f: f.write("order_id,city,amount,status\n101,Pune,1200,paid\n102,Delhi,450,pending\n" "103,Pune,980,paid\n104,Mumbai,,cancelled\n105,Delhi,2300,paid\n") orders = pd.read_csv("orders.csv")print(orders.head(3))print(orders.dtypes["amount"], len(orders))order_id city amount status 0 101 Pune 1200.0 paid 1 102 Delhi 450.0 pending 2 103 Pune 980.0 paid float64 5
Quick ways to inspect a new dataset:
import pandas as pd orders = pd.read_csv("orders.csv")print(orders.columns.tolist())print(orders["amount"].describe().round(1).to_dict())print(orders["city"].value_counts().to_dict())['order_id', 'city', 'amount', 'status']
{'count': 4.0, 'mean': 1232.5, 'std': 778.2, 'min': 450.0, '25%': 847.5, '50%': 1090.0, '75%': 1475.0, 'max': 2300.0}
{'Pune': 2, 'Delhi': 2, 'Mumbai': 1}Select columns and rows
import pandas as pd df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena"], "dept": ["QA", "Dev", "QA"], "salary": [52000, 61000, 48000]}) print(df["name"].tolist()) # one columnprint(df[["name", "salary"]]) # several columnsprint(df.loc[1, "name"]) # by labelprint(df.iloc[0].to_dict()) # by position['Asha', 'Ravi', 'Meena']
name salary
0 Asha 52000
1 Ravi 61000
2 Meena 48000
Ravi
{'name': 'Asha', 'dept': 'QA', 'salary': 52000}Filter rows
import pandas as pd df = pd.DataFrame({ "name": ["Asha", "Ravi", "Meena", "John", "Sara"], "dept": ["QA", "Dev", "QA", "Ops", "Dev"], "salary": [52000, 61000, 48000, 55000, 67000],})print(df[df["salary"] > 55000])print(df[(df["dept"] == "QA") & (df["salary"] > 50000)]["name"].tolist())print(df[df["dept"].isin(["Dev", "Ops"])].shape[0])print(df.query("salary < 50000")["name"].tolist())name dept salary 1 Ravi Dev 61000 4 Sara Dev 67000 ['Asha'] 3 ['Meena']
Sort and add columns
import pandas as pd df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena"], "salary": [52000, 61000, 48000], "years": [4, 6, 2]})df["bonus"] = df["salary"] * 0.1df["level"] = df["years"].apply(lambda y: "senior" if y >= 5 else "junior")print(df.sort_values("salary", ascending=False))name salary years bonus level 1 Ravi 61000 6 6100.0 senior 0 Asha 52000 4 5200.0 junior 2 Meena 48000 2 4800.0 junior
Missing data
import pandas as pd orders = pd.read_csv("orders.csv")print(orders["amount"].isna().sum())print(orders.dropna(subset=["amount"]).shape)filled = orders.fillna({"amount": 0})print(filled["amount"].tolist())1 (4, 4) [1200.0, 450.0, 980.0, 0.0, 2300.0]
Group and aggregate
groupby splits the rows into groups, applies a calculation to each, and combines the results:
import pandas as pd orders = pd.read_csv("orders.csv")paid = orders[orders["status"] == "paid"]print(paid.groupby("city")["amount"].sum())print(orders.groupby("status").size().to_dict())print(paid.groupby("city")["amount"].agg(["count", "mean"]))city
Delhi 2300.0
Pune 2180.0
Name: amount, dtype: float64
{'cancelled': 1, 'paid': 3, 'pending': 1}
count mean
city
Delhi 1 2300.0
Pune 2 1090.0Merge two tables
import pandas as pd users = pd.DataFrame({"user_id": [1, 2, 3], "name": ["Asha", "Ravi", "Meena"]})orders = pd.DataFrame({"order_id": [10, 11, 12, 13], "user_id": [1, 1, 3, 4], "amount": [500, 250, 900, 100]}) joined = orders.merge(users, on="user_id", how="left")print(joined)print(joined.groupby("name")["amount"].sum().to_dict()) order_id user_id amount name
0 10 1 500 Asha
1 11 1 250 Asha
2 12 3 900 Meena
3 13 4 100 NaN
{'Asha': 750, 'Meena': 900}A left merge keeps every order; user 4 has no match, so name is missing (NaN) for that row.
Save results
import pandas as pd orders = pd.read_csv("orders.csv")summary = orders.groupby("city", as_index=False)["amount"].sum()summary.to_csv("city_totals.csv", index=False)print(open("city_totals.csv").read().strip())city,amount Delhi,2750.0 Mumbai,0.0 Pune,2180.0
Check your understanding
Predict the output
import pandas as pddf = pd.DataFrame({"x": [1, 2, 3, 4], "g": ["a", "b", "a", "b"]})print(df[df["x"] > 1]["x"].sum(), df.groupby("g")["x"].sum().to_dict())9 {'a': 4, 'b': 6}The filter keeps 2, 3 and 4, which add up to 9. Grouping by g adds 1 + 3 for “a” and 2 + 4 for “b”.
Quick quiz
1. Which reads a CSV file into a DataFrame?
read_csv handles headers, types and missing values.
2. What is the difference between loc and iloc?
df.loc[5] is the row labelled 5; df.iloc[5] is the sixth row.
3. How do you count missing values in a column?
isna() marks missing values and sum() counts the True values.
Exercise: department report
For each department, print the number of employees and the average salary, sorted by average salary, highest first.
the employee DataFrame from the Series and DataFrame section
count mean dept Dev 2 64000.0 Ops 1 55000.0 QA 2 50000.0
Hint 1
groupby("dept")["salary"].agg(["count", "mean"]), then sort_values("mean", ascending=False).
import pandas as pd df = pd.DataFrame({ "name": ["Asha", "Ravi", "Meena", "John", "Sara"], "dept": ["QA", "Dev", "QA", "Ops", "Dev"], "salary": [52000, 61000, 48000, 55000, 67000],})report = df.groupby("dept")["salary"].agg(["count", "mean"]).sort_values("mean", ascending=False)print(report)count mean dept Dev 2 64000.0 Ops 1 55000.0 QA 2 50000.0
agg runs both calculations on each group in one pass, and sort_values orders the result by the mean column.
Program Practice
95 Python Pandas Programs
Every program has a step-by-step explanation, colour-coded code and its real output.
Pandas FAQ
What is pandas used for in Python?
Loading, cleaning, transforming and summarising tabular data from CSV, Excel, JSON and SQL sources.
What is the difference between a Series and a DataFrame?
A Series is a single labelled column. A DataFrame is a two-dimensional table made of several Series that share an index.
How do I filter rows in pandas?
Use a boolean condition inside brackets: df[df["salary"] > 50000]. Combine conditions with & and | and wrap each one in brackets.
How do I handle missing values in pandas?
Find them with isna(), remove them with dropna(), or replace them with fillna().
2 thoughts on “Python Pandas”