Links

Fill emtpy values

Tags: #pandas #snippet #datacleaning #operations
Author: Florent Ravenel​
Description: This notebook fill empty values in dataframe columns.

Input

Import libraries

import pandas as pd
import numpy as np

Setup DataFrame

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', np.NaN],
'points': [11, 7, 8, 10, np.NaN, 13],
'assists': [5, 7, 7, 9, 12, np.NaN],
'rebounds': [np.NaN, 8, 10, 6, 6, np.NaN]})
df

Define data to fill if empty values in columns

# dict of columns with value to fill
to_fillna = {
'team': "Unknown team",
'points': 0,
'assists': 0,
'rebounds': 0
}

Model

Fill empty values in dataframe

df = df.fillna(to_fillna)

Output

View new DataFrame

df