Bạn đang xem thử công khai
Đăng nhập để lưu tiến độ, làm bài tập, thảo luận và nhận phản hồi.
After this lesson you will be able to: detect and handle missing values, duplicate rows, wrong data types and messy strings — the tasks that take up most of the time in a real data project.
Pandas records an empty cell as NaN. First, count them:
import pandas as pd
s = pd.Series([8.0, None, 6.0, None, 10.0])
print(s.isna().sum()) # 2 (True counts as 1)
print(s.isna().tolist()) # [False, True, False, True, False]
There are two main ways to handle them:
print(s.dropna().tolist()) # [8.0, 6.0, 10.0] ← drop the missing values
print(s.fillna(s.mean()).tolist()) # [8.0, 8.0, 6.0, 8.0, 10.0] ← fill with the mean
Which one to choose depends on the problem: dropping rows loses data; filling with the mean keeps the number of rows but makes the distribution "flatter". When computing statistics, mean() skips NaN by default rather than treating NaN as 0:
print(s.mean()) # 8.0 ← (8 + 6 + 10) / 3
print(s.fillna(0).mean()) # 4.8 ← filling with 0 drags the mean down
d = pd.DataFrame({"id": [1, 2, 2, 3], "v": ["a", "b", "b", "c"]})
print(d.duplicated().sum()) # 1 (the second row of the duplicate pair)
print(len(d.drop_duplicates())) # 3 (keeps the first occurrence)
A numeric column that was read as text (because of stray characters) cannot be used in calculations. Use astype when you are sure the data is clean, or to_numeric when corrupted values may be mixed in:
print(pd.Series(["1", "2", "3"]).astype(int).sum()) # 6
t = pd.to_numeric(pd.Series(["1", "2", "x"]), errors="coerce")
print(t.tolist()) # [1.0, 2.0, nan] ← "x" cannot be converted, so it becomes NaN
errors="coerce" turns corrupted values into NaN so you can handle them next with fillna/dropna.
names = pd.Series([" An ", "BINH"])
print(names.str.strip().str.lower().tolist()) # ['an', 'binh']
.str.strip() removes whitespace at both ends and .str.lower() converts to lowercase. This way "IT", "it" and " IT" are no longer counted as three different groups when you compute statistics. You can chain several steps together with dots.
To replace specific values: s.replace("N/A", None) turns the string "N/A" into a missing value.
df.info(), df.isna().sum(), df.describe().to_numeric, astype)..str.strip().str.lower()).dropna or fillna).drop_duplicates).shape and isna().sum().isna().sum(); mean() skips NaN.dropna removes rows, fillna fills in a value.drop_duplicates keeps the first occurrence.to_numeric(..., errors="coerce") turns corrupted values into NaN.McKinney, W. (2022). Python for Data Analysis: Data Wrangling with Pandas, NumPy, and Jupyter (3rd ed.). O'Reilly Media.