pandas dataframe dataframe.fillna() 函数
pandas.dataframe.fillna() 函数将 dataframe 中的 nan 值替换为某个值。
pandas.dataframe.fillna() 语法
dataframe.fillna(
value=none, method=none, axis=none, inplace=false, limit=none, downcast=none
)
参数
value |
scalar、dict、series 或 dataframe。用于替换 nan 的值 |
method |
backfill、bfill、pad、ffill 或 none。用于填充 nan 值的方法 |
axis |
沿行(axis=0)或列(axis=1)填补缺失的数值 |
inplace |
布尔型。如果为 true,就地修改调用者 dataframe |
limit |
整数。如果指定了 method,则是要向前/向后填充的连续 nan 值的最大数量。如果没有指定 method,则是要填充的轴的最大 nan 值数 |
downcast |
字典。指定转换的数据类型 |
返回值
如果 inplace 为 true,则用给定的 value 替换所有 nan 值的 dataframe;否则为 none。
示例代码:用 dataframe.fillna() 方法填充所有 dataframe 中的 nan 值
import pandas as pd
import numpy as np
df = pd.dataframe({'x': [1, 2, 3, np.nan, 3],
'y': [4, np.nan, 8, np.nan, 3]})
print("dataframe:")
print(df)
filled_df = df.fillna(5)
print("filled dataframe:")
print(filled_df)
输出:
dataframe:
x y
0 1.0 4.0
1 2.0 nan
2 3.0 8.0
3 nan nan
4 3.0 3.0
filled dataframe:
x y
0 1.0 4.0
1 2.0 5.0
2 3.0 8.0
3 5.0 5.0
4 3.0 3.0
它用 pandas.dataframe.fillna() 方法中作为参数提供的 5 填充 dataframe 中的所有 nan 值。
dataframe.fillna() 中的平均数
我们可以用一列的平均值来代替该列的 nan 值。
import pandas as pd
import numpy as np
df = pd.dataframe({'x': [1, 2, 3, np.nan, 3],
'y': [4, np.nan, 8, np.nan, 3]})
print("dataframe:")
print(df)
df.fillna(df.mean(),inplace=true)
print("filled dataframe:")
print(df)
输出:
dataframe:
x y
0 1.0 4.0
1 2.0 nan
2 3.0 8.0
3 nan nan
4 3.0 3.0
filled dataframe:
x y
0 1.00 4.0
1 2.00 5.0
2 3.00 8.0
3 2.25 5.0
4 3.00 3.0
它将 x 列的 nan 值用 x 列的平均值填充,y 列的 nan 值用 y 列的平均值填充。
由于 inplace=true,调用 fillna() 函数后,原 dataframe 被修改。
dataframe.fillna() 用 0 来填充
import pandas as pd
import numpy as np
df = pd.dataframe({'x': [1, 2, 3, np.nan, 3],
'y': [4, np.nan, 8, np.nan, 3]})
print("dataframe:")
print(df)
df.fillna(0,inplace=true)
print("filled dataframe:")
print(df)
输出:
dataframe:
x y
0 1.0 4.0
1 2.0 nan
2 3.0 8.0
3 nan nan
4 3.0 3.0
filled dataframe:
x y
0 1.0 4.0
1 2.0 0.0
2 3.0 8.0
3 0.0 0.0
4 3.0 3.0
它用 0 填充所有 nan。
示例代码:dataframe.fillna() 方法,参数为 method
我们也可以使用不同的 “方法 “参数在 dataframe 中填充 nan 值。
import pandas as pd
import numpy as np
df = pd.dataframe({'x': [1, 2, 3, np.nan, 3],
'y': [4, np.nan, 8, np.nan, 3]})
print("dataframe:")
print(df)
filled_df = df.fillna(method="backfill")
print("filled dataframe:")
print(filled_df)
输出:
dataframe:
x y
0 1.0 4.0
1 2.0 nan
2 3.0 8.0
3 nan nan
4 3.0 3.0
filled dataframe:
x y
0 1.0 4.0
1 2.0 8.0
2 3.0 8.0
3 3.0 3.0
4 3.0 3.0
设置 method="backfill" 将所有的 dataframe 中的 nan 值填充到同一列的 nan 值之后。
我们也可以使用 bfill、pad 和 ffill 方法来填充 dataframe 中的 nan 值。
method 方法 |
说明 |
|---|---|
backfill/bfill |
用同一列中的 nan 值之后的值填充 dataframe 中所有的 nan 值 |
ffill/pad |
用同一列中的 nan 值之前的值填充 dataframe 中所有的 nan 值 |
示例代码:dataframe.fillna() 方法的 limit 参数
dataframe.fillna() 方法中的 limit 参数限制了该方法所要填充的连续 nan 值的最大数量。
import pandas as pd
import numpy as np
df = pd.dataframe({'x': [1, 2,np.nan, 3,3],
'y': [4, np.nan, 8, np.nan, 3]})
print("dataframe:")
print(df)
filled_df = df.fillna(3,limit=1)
print("filled dataframe:")
print(filled_df)
输出:
dataframe:
x y
0 1.0 4.0
1 2.0 nan
2 nan 8.0
3 3.0 nan
4 3.0 3.0
filled dataframe:
x y
0 1.0 4.0
1 2.0 3.0
2 3.0 8.0
3 3.0 nan
4 3.0 3.0
在这里,一旦一列中的 nan 值被填满,同一列中的其他 nan 值将保持原样。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串

