How to fill missing values in pandas?

Published on Aug. 22, 2023, 12:18 p.m.

You can fill missing values in pandas using the fillna() method. fillna() provides several options for how to fill the missing values. Here are a few examples:

  1. Fill with a specific value:

import pandas as pd

create a sample dataframe with missing values

df = pd.DataFrame({‘A’: [1, 2, None, 4], ‘B’: [5, 6, 7, None]})

fill missing values with 0

df = df.fillna(0)

print(df)


This will output a new DataFrame with all missing values replaced with 0:

 A    B

0 1.0 5.0
1 2.0 6.0
2 0.0 7.0
3 4.0 0.0


2. Fill with the mean value of the column:

fill missing values with mean of column

df = df.fillna(df.mean())

print(df)


This will output a new DataFrame with all missing values replaced with the mean value of the respective columns:

 A    B

0 1.0 5.0
1 2.0 6.0
2 2.333333 7.0
3 4.0 6.0


3. Forward fill missing values (propagate non-null values forward):

forward fill missing values

df = df.fillna(method=’ffill’)

print(df)


This will output a new DataFrame with all missing values filled with the previous non-null value in their respective columns:

 A    B

0 1.0 5.0
1 2.0 6.0
2 2.0 7.0
3 4.0 7.0


4. Backward fill missing values (propagate non-null values backward):

backward fill missing values

df = df.fillna(method=’bfill’)

print(df)


This will output a new DataFrame with all missing values filled with the next non-null value in their respective columns:

 A    B

0 1.0 5.0
1 2.0 6.0
2 4.0

Tags: