Cookbook | Data Cleaning

Published and available on GitHub as part of the Python Cookbook Repository: fillna_cat_num_values.ipynb
Filling missing categorical and numerical data with fillna(), mode() and statistical measures like mean() or median().
Topics
| UseCase | Cookbook recipe |
| Categorical Data | Select Columns in DataFrame manually. Fill NA/NaN with ‚mode‘, in case of equal take first index. |
| Numerical Data | Select Columns in DataFrame manually. Fill NA/Nan with mean.’Select Columns in DataFrame manually. Fill NA/Nan with ‚mean‘. |
| Mixed Data | Select Columns in DataFrame by Dtype. Fill NA/NaN with ‚mode or mean‘. |
UseCase: categorical data
Demo data
is a pandas dataframe with categorical data and missing values.
Objective
is to fill the missing values, with the most occurring value.
Notes
In case we have equally occurring values, we just pick the first.
–> .mode()[0]
import pandas as pd
import numpy as np
# Create simple dictionary with some missing values
data = {
'Name': ['John', 'Anna', np.nan, 'Linda', 'John'],
'Type': ['Type1', 'Type2', 'Type2', np.nan, 'Type1'],
'Country': ['Country1', np.nan, 'Country2', 'Country1', 'Country2'],
}
# Convert dictionary to pandas DataFrame
data = pd.DataFrame(data)
print("Original DataFrame:")
print(data)
# Apply mode and fillna
for column in ['Name', 'Type', 'Country']:
mode = data[column].mode()[0] # in case we have more then one mode
data[column].fillna(mode, inplace=True)
print("\nDataFrame after filling NA values with mode:")
print(data)
Output:
# Original DataFrame:
Name Type Country
0 John Type1 Country1
1 Anna Type2 NaN
2 NaN Type2 Country2
3 Linda NaN Country1
4 John Type1 Country2
# DataFrame after filling NA values with mode:
Name Type Country
0 John Type1 Country1
1 Anna Type2 Country1
2 John Type2 Country2
3 Linda Type1 Country1
4 John Type1 Country2
UseCase: numerical data
Demo data
is a pandas dataframe with numerical data and missing values.
Objective
is to fill the missing values, with the median
import pandas as pd
import numpy as np
# Sample data with some missing values
data = {
'Turnover': [100, 200, np.nan, 400, 500, 600, np.nan, 800],
'Transactions': [1, 2, 3, np.nan, np.nan, 6, 7, 8]
}
# Convert dictionary to a pandas DataFrame
data = pd.DataFrame(data)
print("Original DataFrame:")
print(data)
# Fill missing values with column median
for column in ['Turnover', 'Transactions']:
median = data[column].median()
data[column].fillna(median, inplace=True)
print("\nDataFrame after filling NA values with median:")
print(data)
Output:
# Original DataFrame:
Turnover Transactions
0 100.0 1.0
1 200.0 2.0
2 NaN 3.0
3 400.0 NaN
4 500.0 NaN
5 600.0 6.0
6 NaN 7.0
7 800.0 8.0
# DataFrame after filling NA values with median:
Turnover Transactions
0 100.0 1.0
1 200.0 2.0
2 450.0 3.0
3 400.0 4.5
4 500.0 4.5
5 600.0 6.0
6 450.0 7.0
7 800.0 8.0
UseCase: Mixed data (numerical, categorical)
import pandas as pd
import numpy as np
# Simple DataFrame with both numerical and categorical data
data = {
'Age': [25, 30, 35, np.nan, 45],
'City': ['New York', 'Seattle', 'San Francisco', 'Austin', np.nan],
'Income': [50000, 70000, np.nan, 90000, 100000]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
def fill_na_with_mean(df, num_cols):
mean_values = df[num_cols].mean()
df[num_cols] = df[num_cols].fillna(mean_values)
return df
def fill_na_with_mode(df, cat_cols):
mode_values = df[cat_cols].mode().iloc[0]
df[cat_cols] = df[cat_cols].fillna(mode_values)
return df
def clean_data(df):
# Identify numerical columns and fill NA/NaN with mean
num_cols = df.select_dtypes(include=np.number).columns
df = fill_na_with_mean(df, num_cols)
# Identify categorical columns and fill NA/NaN with mode
cat_cols = df.select_dtypes(include='object').columns
df = fill_na_with_mode(df, cat_cols)
return df
# Apply the function on our DataFrame
cleaned_df = clean_data(df)
print("\nCleaned DataFrame:")
print(cleaned_df)
Output:
# Original DataFrame:
Age City Income
0 25.0 New York 50000.0
1 30.0 Seattle 70000.0
2 35.0 San Francisco NaN
3 NaN Austin 90000.0
4 45.0 NaN 100000.0
# Cleaned DataFrame:
Age City Income
0 25.00 New York 50000.0
1 30.00 Seattle 70000.0
2 35.00 San Francisco 77500.0
3 33.75 Austin 90000.0
4 45.00 Austin 100000.0
mode()
The mode() function is part of the pandas library in Python. It is used to find the mode (most frequently occurring value) in a pandas Series or DataFrame.
fillna()
The fillna() function is part of the pandas library in Python. This function is used to fill NA/NaN values using the specified method.



