- 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"])
Pandas Cleaning Data
Missing values, duplicates and wrong types.
✍️ 3 exercises + code challenge in this lessonKey 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
Pandasimport 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)
[1, 2] name age 0 Ali 25.0 1 Sara 27.5
Pandas Cleaning Data Practice Exercises
Check what you learned in “Pandas Cleaning Data”.
Remove rows that contain missing values.
df = df.()
Which method removes duplicate rows?
Fill the missing ages with 0 and print the ages as a list of ints.
Free sign-in required · Google login available
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