Data Cleaning with Pandas
✕Common Data Problems
Common Data Quality Issues in Real-World Datasets:
| Issue | Resolution |
|---|---|
| Inconsistent Column Names | Standardize names using rename / load with names parameter. |
Missing Values (NULL, Blanks) | Use dropna to remove rows / fillna to fill them. |
| Duplicates | Use drop_duplicates to remove duplicate rows. |
Inconsistent Categories. F, Fe | Standardize category names using replace or map. |
| Incorrect Data Types | Use astype to convert columns to correct data types. |
| Outliers | Identify outliers and remove with filter / clip. |
| Incorrect data format | .melt, .pivot |
| Spelling Mistakes | Fuzzy matching |
| Inconsistent Units | Unit conversion to standardize measurements. |
| Case Sensitivity | Convert text to lower or upper case for consistency. |
Business Rule. TxnDate > Created | Fix / Remove data to align with business rule |
| Leading and Trailing Spaces | Use strip() to remove leading and trailing spaces. |
| Non Numeric Character in numeric column | Use regex to clean non-numeric characters. |
Common data quality issues in real-world datasets, their descriptions, and potential resolutions
Tidy Data
- Standard way of organizing data to facilitate analysis.
- Each variable forms a column, each observation forms a row, and each type of observational unit forms a table.
Example of Untidy Data
| product | year_2020_sales | year_2021_sales |
|---|---|---|
| Product A | 100 | 150 |
| Product B | 200 | 250 |
Example of untidy data where sales for different years are in separate columns.
Example of Tidy Data
| product | year | sales |
|---|---|---|
| Product A | 2020 | 100 |
| Product A | 2021 | 150 |
| Product B | 2020 | 200 |
| Product B | 2021 | 250 |
Example of tidy data where each variable (year, sales) forms a column and each observation forms a row.
melt
- Tidy data is better for analysis, untidy is better for reporting.
- melt converts wide format data to long format. i.e
tidy format - Number of rows increases and number of columns decreases.
- Syntax:
pd.melt(df, id_vars=["id_1", "id_2"], value_vars=["val_1", "val_2"]) - Example:
import pandas as pd df = pd.DataFrame({ "product": ["A", "B"], "year_2020_sales": [100, 200], "year_2021_sales": [150, 250] }) melted_df = pd.melt(df, id_vars=["product"]) print(melted_df)
pivot_table
- Pivoting is the reverse of melting.
- It converts long format data to wide format.
- Number of rows decreases and number of columns increases.
- Aggregation for duplicate is average. Can be customized with
aggfunc. - Syntax:
df.pivot_table(index=["id_1", "id_2"], columns="variable", values="value") - Example:
import pandas as pd df = pd.DataFrame({ "product": ["A", "A", "B", "B"], "year": [2020, 2021, 2020, 2021], "sales": [100, 150, 200, 250] }) p_df = df.pivot_table(index="product", columns="year", values="sales") print(p_df)
cut and qcut
cut: Segment and sort data values into bins.qcut: Segment & sort data values into equal-sized bins based on quantiles.bins = [0, 18, 35, 60] labels = ["Child", "Adult", "Senior"] df["age_group"] = pd.cut(df["age"], bins=bins, labels=labels) print(df) labels = ["Young", "Middle-aged", "Old"] df["age_quantile"] = pd.qcut(df["age"], q=3, labels=labels) print(df)
Example
Handling Missing Value
- Dropping rows/columns with missing values.
- Filling missing values with statistics (mean, median, mode).
- Use techniques like interpolation, prediction to estimate missing values.
- Example:
df["course_clean"] = df["course"].fillna("Unknown") df["age_clean"] = df["age"].fillna(df["age"].mean()) df["experience_clean"] = df["experience"].ffill()# or bfillfilter_mask = (df["education"].isnull()) & (df["age"] < 25) df.loc[filter_mask, "education"] = "SLC" df.dropna(subset=["name"], inplace=True)
Common Strategies for Handling Missing Values
Handling Duplicates
- Duplicate rows can be identified with
duplicated()function. - Duplicate rows can be removed with
drop_duplicates()function. duplicate_mask = df.duplicated(subset=["name", "age"], keep=False) duplicates = df[duplicate_mask] print(duplicates) # Remove duplicates based on name and age columns unique_df = df.drop_duplicates(subset=["name", "age"], keep="first") print(unique_df.duplicated(subset=["name", "age"]).sum()
Example
Data Value issues
df["gender"] = df["gender"].replace({"m": "Male", "f": "Female"}) mapping_dict = {"m": "Male", "f": "Female"} df["gender"] = df["gender"].map(mapping_dict)df["age"] = df["age"].clip(lower=0, upper=120) outlier_mask = (df["age"] < 0) | (df["age"] > 120) df = df[~outlier_mask]df["name"] = df["name"].str.strip()df["name"] = df["name"].str.upper()# ordf["name"] = df["name"].str.lower()df["age"] = df["age"].str.replace(r"\D", "", regex=True).astype(int)
Inconsistent Categories (map/replace)
Outliers (clip/drop)
Leading and Trailing Spaces (strip)
Inconsistent Case
Non Numeric Character in numeric column (regex)
Fuzzy Matching to clean Spelling issue
- Technique to find strings that are approximately equal instead of exact.
- Match result is based on similarity score.
- The
fuzzywuzzylibrary can be used for fuzzy matching in Python. from fuzzywuzzy import process choices = ["New York", "Kathmandu", "Delhi"] query = "New yolk" best_match = process.extractOne(query, choices) print(best_match)
Example
Practice QuestionsNot started
Data Transformation
Question 1 of 4
- Load
corona_data.csvinto a pandas DataFrame. Transform this data to have columnsDate,Country,TotalCases,TotalDeaths. Save result in csv filecorona_transformed.csv. - Load
lab_reading.csvinto a pandas DataFrame. Transform this data to have columnsDate,CO2,RainandMethane. FillReadingin appropriate column and save result in csv filelab_reading_transformed.csv. Note: Incase of duplicate take min value. - Load
treatment_info.csvinto a pandas DataFrame. Transform this data to have columnsDate,Treatment TypeandDosage. Save result in csv filetreatment_info_transformed.csv.
- Load
Data Cleaning - Heart Disease Dataset
Question 2 of 4
- Load
heart_disease_raw.csvinto a DataFrame. Perform the following operations: 1. Display number of rows and columns in dataset. 2. Display column names and their datatypes. 3. Rename columnHeart_ stroketoHeart_Stroke. 4. Display sample 15 records. 5. AdjustGendercolumn to have onlyM,Fandnull. 6. Adjusteducationto have only:Uneducate,Primary School,Graduate,Post Graduate,null7. AdjustExerciseto have only:null,daily,weeklyandmonthly8. Fill missing value in numeric column with their mean. 9. Fill missing value in categorical column with most frequent value. 10. Remove duplicate records. 11. Replace outliers in numeric column with mean value. 12. EnsureGender,education,Exercise,prevalentStroke,Heart_Strokeis category dtype 13. Ensure other columns also have appropriate datatypes. 14. Save cleaned data asheart_disease_cleaned.csv.
- Load
Data Cleaning - Baseball Player Dataset
Question 3 of 4
- Load
Baseball player.txtinto a DataFrame. Perform the following operations: 1. During Load assign header from fileBaseball Player - Clean.txt. 2. Display sample 15 records to understand data. 3. Handle missing value in numeric column with mean & categorical column with mode 4. Remove duplicate records. 5. Handle outliers in numeric column by clipping to 1st & 99th percentile. 6. Drop duplicate records. 7. Create new columnheight_cmconverting existing inch value1 inch = 2.54 cm8. Bin players into age group as:10-15,15-20, and so on till last value is included. 9. Replace underscore (_) of position column by space 10. Split the Name asFirstName,MiddleName,LastName& store in three columns. 11. For non-numeric value check if there is casing and spelling mistake. If so fix it. 12. Ensure all columns have appropriate datatypes. 13. Save cleaned data as tab separated values inbaseball_player_cleaned.tsv.
- Load
Answering from Baseball Player Dataset
Question 4 of 4
- Load
baseball_player_cleaned.tsvinto a DataFrame. Answer the following questions: 1. Which player has the highest BMI? (BMI = weight / height_cm^2) 2. Which team has the highest average player age? 3. How many players are in each position category? 4. What is the average weight of players in each age group? 5. Which player has the longest name (in terms of characters)? 6. How many players have age above 30 and weight below 70? 7. What is the distribution of players across different age groups? 8. Which player has the highest number of characters in their name? 9. How many players haveCenteras their position and are in20-25age group? 10. What is the average height of players in each position category? 11. What is average Height & Weight of U-23 baseball players? 12. Display the most common position hired by each team. 13.Display average age of each team and Position 14. Display MinHeight, MaxHeight, AverageHeight, MinWeight, MaxWeight & AverageWeight for each Position.
- Load
