深度阅读

How to fill missing values in pandas?

作者
作者
2023年08月22日
更新时间
17.35 分钟
阅读时间
0
阅读量

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

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。