the full guide

Pandas: working with tables

Load, filter, group and clean data like a spreadsheet you can program.

15 min Beginner

What you need: The earlier pages of this course. Nothing to install.

A DataFrame is a table

pandas_basics.py
import pandas as pd

df = pd.DataFrame({
    "name": ["Riya", "Aman", "Sara", "Dev"],
    "city": ["Pune", "Delhi", "Pune", "Delhi"],
    "marks": [78, 92, 65, 40],
})

print(df.head(2))
print(df.shape)
print(df["marks"].mean())
print(df[df["marks"] >= 70])
print(df.sort_values("marks", ascending=False).iloc[0]["name"])

Output

   name   city  marks
0  Riya   Pune     78
1  Aman  Delhi     92
(4, 3)
68.75
   name   city  marks
0  Riya   Pune     78
1  Aman  Delhi     92
Aman

Grouping and new columns

pandas_group.py
import pandas as pd

df = pd.DataFrame({
    "city": ["Pune", "Delhi", "Pune", "Delhi"],
    "marks": [78, 92, 65, 40],
})

print(df.groupby("city")["marks"].mean())

df["passed"] = df["marks"] >= 50
print(df)
print(df["passed"].value_counts())

Output

city
Delhi    66.0
Pune     71.5
Name: marks, dtype: float64
    city  marks  passed
0   Pune     78    True
1  Delhi     92    True
2   Pune     65    True
3  Delhi     40   False
passed
True     3
False    1
Name: count, dtype: int64

Missing data

pandas_clean.py
import pandas as pd
import numpy as np

df = pd.DataFrame({"age": [14, np.nan, 16], "marks": [78, 85, np.nan]})

print(df.isna().sum())           # How many are missing per column
print(df.dropna())               # Drop rows with gaps
print(df.fillna(df.mean()))      # Or fill with the average

Output

age      1
marks    1
dtype: int64
    age  marks
0  14.0   78.0
    age  marks
0  14.0   78.0
1  15.0   85.0
2  16.0   81.5

Finished reading?