pandas complete guide
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Complete Pandas Guide (Beginner → Advanced)
This notebook is a comprehensive guide to Pandas, the most widely used Python library for data analysis.
Topics covered:
- Introduction to Pandas
- Series
- DataFrames
- Data Loading (CSV, Excel, JSON)
- Inspecting Data
- Indexing & Selection
- Filtering Data
- Handling Missing Values
- Sorting
- GroupBy Operations
- Aggregations
- Merging & Joining
- Pivot Tables
- Time Series
- String Operations
- Applying Functions
- Window Operations
- Performance Tips
- Visualization
- Real Data Analysis Example
Run the code cells sequentially to explore each concept.
import pandas as pd
import numpy as np
print("Pandas version:", pd.__version__)
Pandas version: 2.1.4
Creating a Series
Explanation and example.
import pandas as pd
data = [10, 20, 30, 40]
s = pd.Series(data)
print(s)
0 10
1 20
2 30
3 40
dtype: int64
Exercise
Try modifying the dataset and explore additional Pandas methods.
Series with Index
Explanation and example.
import pandas as pd
data = [10,20,30]
s = pd.Series(data, index=["a","b","c"])
print(s)
print(s["b"])
a 10
b 20
c 30
dtype: int64
20
Exercise
Try modifying the dataset and explore additional Pandas methods.
Creating DataFrames
Explanation and example.
import pandas as pd
data = {
"name": ["Alice","Bob","Charlie"],
"age": [25,30,35],
"city": ["NY","London","Paris"]
}
df = pd.DataFrame(data)
print(df)
name age city
0 Alice 25 NY
1 Bob 30 London
2 Charlie 35 Paris
Exercise
Try modifying the dataset and explore additional Pandas methods.
Inspecting Data
Explanation and example.
print(df.head())
print(df.tail())
print(df.info())
print(df.describe())
name age city
0 Alice 25 NY
1 Bob 30 London
2 Charlie 35 Paris
name age city
0 Alice 25 NY
1 Bob 30 London
2 Charlie 35 Paris
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 3 non-null object
1 age 3 non-null int64
2 city 3 non-null object
dtypes: int64(1), object(2)
memory usage: 200.0+ bytes
None
age
count 3.0
mean 30.0
std 5.0
min 25.0
25% 27.5
50% 30.0
75% 32.5
max 35.0
Exercise
Try modifying the dataset and explore additional Pandas methods.
Selecting Columns
Explanation and example.
print(df["name"])
print(df[["name","age"]])
0 Alice
1 Bob
2 Charlie
Name: name, dtype: object
name age
0 Alice 25
1 Bob 30
2 Charlie 35
Exercise
Try modifying the dataset and explore additional Pandas methods.
Row Selection with loc
Explanation and example.
print(df.loc[0])
print(df.loc[0:1])
name Alice
age 25
city NY
Name: 0, dtype: object
name age city
0 Alice 25 NY
1 Bob 30 London
Exercise
Try modifying the dataset and explore additional Pandas methods.
Row Selection with iloc
Explanation and example.
print(df.iloc[0])
print(df.iloc[0:2])
name Alice
age 25
city NY
Name: 0, dtype: object
name age city
0 Alice 25 NY
1 Bob 30 London
Exercise
Try modifying the dataset and explore additional Pandas methods.
Filtering Data
Explanation and example.
filtered = df[df["age"] > 25]
print(filtered)
name age city
1 Bob 30 London
2 Charlie 35 Paris
Exercise
Try modifying the dataset and explore additional Pandas methods.
Adding Columns
Explanation and example.
df["salary"] = [50000,60000,70000]
print(df)
name age city salary
0 Alice 25 NY 50000
1 Bob 30 London 60000
2 Charlie 35 Paris 70000
Exercise
Try modifying the dataset and explore additional Pandas methods.
Dropping Columns
Explanation and example.
df2 = df.drop(columns=["city"])
print(df2)
name age salary
0 Alice 25 50000
1 Bob 30 60000
2 Charlie 35 70000
Exercise
Try modifying the dataset and explore additional Pandas methods.
Handling Missing Data
Explanation and example.
df_missing = pd.DataFrame({
"A":[1,2,None],
"B":[4,None,6]
})
print(df_missing)
print(df_missing.isna())
print(df_missing.fillna(0))
A B
0 1.0 4.0
1 2.0 NaN
2 NaN 6.0
A B
0 False False
1 False True
2 True False
A B
0 1.0 4.0
1 2.0 0.0
2 0.0 6.0
Exercise
Try modifying the dataset and explore additional Pandas methods.
Sorting Data
Explanation and example.
print(df.sort_values("age"))
name age city salary
0 Alice 25 NY 50000
1 Bob 30 London 60000
2 Charlie 35 Paris 70000
Exercise
Try modifying the dataset and explore additional Pandas methods.
GroupBy Example
Explanation and example.
data = {
"team":["A","A","B","B","B"],
"points":[10,20,30,40,50]
}
df_group = pd.DataFrame(data)
grouped = df_group.groupby("team").mean()
print(grouped)
points
team
A 15.0
B 40.0
Exercise
Try modifying the dataset and explore additional Pandas methods.
Aggregations
Explanation and example.
print(df_group.groupby("team").agg(["mean","sum","count"]))
points
mean sum count
team
A 15.0 30 2
B 40.0 120 3
Exercise
Try modifying the dataset and explore additional Pandas methods.
Merging DataFrames
Explanation and example.
left = pd.DataFrame({"id":[1,2,3],"name":["Alice","Bob","Charlie"]})
right = pd.DataFrame({"id":[1,2,3],"salary":[50000,60000,70000]})
merged = pd.merge(left,right,on="id")
print(merged)
id name salary
0 1 Alice 50000
1 2 Bob 60000
2 3 Charlie 70000
Exercise
Try modifying the dataset and explore additional Pandas methods.
Concatenation
Explanation and example.
df1 = pd.DataFrame({"A":[1,2]})
df2 = pd.DataFrame({"A":[3,4]})
combined = pd.concat([df1,df2])
print(combined)
A
0 1
1 2
0 3
1 4
Exercise
Try modifying the dataset and explore additional Pandas methods.
Pivot Tables
Explanation and example.
data = {
"city":["NY","NY","London","London"],
"year":[2020,2021,2020,2021],
"sales":[100,120,90,130]
}
df_pivot = pd.DataFrame(data)
print(df_pivot)
pivot = df_pivot.pivot_table(values="sales",index="city",columns="year")
print(pivot)
city year sales
0 NY 2020 100
1 NY 2021 120
2 London 2020 90
3 London 2021 130
year 2020 2021
city
London 90.0 130.0
NY 100.0 120.0
Exercise
Try modifying the dataset and explore additional Pandas methods.
Time Series
Explanation and example.
dates = pd.date_range("2023-01-01", periods=5)
ts = pd.Series(range(5), index=dates)
print(ts)
2023-01-01 0
2023-01-02 1
2023-01-03 2
2023-01-04 3
2023-01-05 4
Freq: D, dtype: int64
Exercise
Try modifying the dataset and explore additional Pandas methods.
String Operations
Explanation and example.
names = pd.Series(["alice","bob","charlie"])
print(names.str.upper())
print(names.str.contains("a"))
0 ALICE
1 BOB
2 CHARLIE
dtype: object
0 True
1 False
2 True
dtype: bool
Exercise
Try modifying the dataset and explore additional Pandas methods.
Apply Functions
Explanation and example.
df["age_plus_10"] = df["age"].apply(lambda x: x+10)
print(df)
name age city salary age_plus_10
0 Alice 25 NY 50000 35
1 Bob 30 London 60000 40
2 Charlie 35 Paris 70000 45
Exercise
Try modifying the dataset and explore additional Pandas methods.
Rolling Window
Explanation and example.
s = pd.Series([1,2,3,4,5,6])
print(s.rolling(3).mean())
0 NaN
1 NaN
2 2.0
3 3.0
4 4.0
5 5.0
dtype: float64
Exercise
Try modifying the dataset and explore additional Pandas methods.
Basic Visualization
Explanation and example.
import matplotlib.pyplot as plt
df_group.groupby("team").sum()["points"].plot(kind="bar")
plt.show()
Exercise
Try modifying the dataset and explore additional Pandas methods.
Saving Data
Explanation and example.
df.to_csv("data.csv",index=False)
loaded = pd.read_csv("data.csv")
print(loaded)
name age city salary age_plus_10
0 Alice 25 NY 50000 35
1 Bob 30 London 60000 40
2 Charlie 35 Paris 70000 45
Exercise
Try modifying the dataset and explore additional Pandas methods.
Mini Data Analysis Example
Explanation and example.
data = {
"product":["A","B","A","B","C","A"],
"sales":[100,200,150,180,90,220]
}
df_sales = pd.DataFrame(data)
summary = df_sales.groupby("product")["sales"].sum()
print(summary)
product
A 470
B 380
C 90
Name: sales, dtype: int64
Exercise
Try modifying the dataset and explore additional Pandas methods.
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →