Links

Fill emtpy values

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

Input

Import libraries

import pandas as pd
import numpy as np

Setup Variables

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

Model

Create 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

Fill empty values in dataframe

df = df.fillna(to_fillna)

Output

Display new DataFrame

df