Sahaj.blogs
12 Mar 2024 · StudyPhase 4 · Pandas

Pandas · Cleaning data

Fixing messy columns: stripping and normalising strings, converting types with to_numeric, renaming, dropping duplicates and mapping categories.

Real data is messy. My cleaning checklist:

  1. Column names: lower-case and strip them, replace spaces with _.
  2. Strings: the .str accessor applies string methods to a whole column (.str.strip().str.title()).
  3. Types: pd.to_numeric(col, errors="coerce") turns bad values into NaN instead of raising.
  4. Categories: .map({...}) or .replace to unify spellings.
  5. Duplicates: drop_duplicates(subset=...).
import pandas as pd

df = pd.DataFrame({
    " Student Name ": ["  asha ", "RAVI", "Sahaj", "Sahaj", "meet"],
    "Branch": ["cse", "I.C.T", "ict", "ict", "Mech"],
    "Marks": ["88", "79", "95", "95", "absent"],
})
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
df["student_name"] = df["student_name"].str.strip().str.title()
df["branch"] = df["branch"].str.lower().str.replace(".", "", regex=False).map(
    {"cse": "CSE", "ict": "ICT", "mech": "ME"})
df["marks"] = pd.to_numeric(df["marks"], errors="coerce")
print(df)
df = df.drop_duplicates()
print(df.dtypes.to_dict())
print(df)

Output:

  student_name branch  marks
0         Asha    CSE   88.0
1         Ravi    ICT   79.0
2        Sahaj    ICT   95.0
3        Sahaj    ICT   95.0
4         Meet     ME    NaN
{'student_name': <StringDtype(storage='python', na_value=nan)>, 'branch': <StringDtype(storage='python', na_value=nan)>, 'marks': dtype('float64')}
  student_name branch  marks
0         Asha    CSE   88.0
1         Ravi    ICT   79.0
2        Sahaj    ICT   95.0
4         Meet     ME    NaN

#pandas#cleaning

Sources

  1. Working with text data — pandas
  2. pandas.to_numeric — pandas
  3. pandas.DataFrame.drop_duplicates — pandas