Pandas Cleaning Data

Missing values, duplicates and wrong types.

✍️ 3 exercises + code challenge in this lesson
  • Find missing values: df.isna().sum()
  • Remove them: df.dropna(); fill them: df.fillna(value)
  • Duplicates: df.duplicated(), df.drop_duplicates()
  • Fix types: df["col"].astype(int), pd.to_datetime(df["date"])

Key Takeaways: Pandas Cleaning Data

Mastering Pandas Cleaning Data helps you write clean and reliable Pandas code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Pandas Cleaning Data Code Example

Pandas
import pandas as pd

df = pd.DataFrame({
    "name": ["Ali", "Sara", None, "Sara"],
    "age": [25, None, 30, None],
})
print(df.isna().sum().tolist())
df["age"] = df["age"].fillna(df["age"].mean())
df = df.dropna(subset=["name"]).drop_duplicates()
print(df)
Output
[1, 2]
   name   age
0   Ali  25.0
1  Sara  27.5
Free sign-in required · Google login available

Pandas Cleaning Data Practice Exercises

Check what you learned in “Pandas Cleaning Data”.

/ 3 solved
Fill in the blank

Remove rows that contain missing values.

df = df.()
✓ Correct! Not quite. Try again.
Multiple choice

Which method removes duplicate rows?

✓ Correct! Not quite. Try again.
Code challenge

Fill the missing ages with 0 and print the ages as a list of ints.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services