pandas dataframe dataframe.interpolate()函数
python pandas dataframe.interpolate() 函数使用插值技术在 dataframe 中填充 nan 值。
pandas.dataframe.interpolate() 语法
dataframe.interpolate(
method="linear",
axis=0,
limit=none,
inplace=false,
limit_direction="forward",
limit_area=none,
downcast=none,
**kwargs
)
参数
method |
linear, time, index, values, nearest, zero, slinear, quadratic, cubic, barycentric, krogh, polynomial, spline, piecewise_polynomial, from_derivatives, pchip, akima 或 none。用于插值 nan 的方法。 |
axis |
沿行(axis=0)或列(axis=1)插补缺失的数值 |
limit |
要内插的最大连续 nan 数 |
inplace |
布尔型。如果 true,就地修改调用方 dataframe。 |
limit_direction |
forward, backward 或 both。当指定 limit 时,将沿 nans 的 direction 进行插值。 |
limit_area |
none, inside 或 outside。当指定 limit 时,对插值的限制。 |
downcast |
字典。指定向下转换数据类型 |
**kwargs |
插值函数的关键字 |
返回值
如果 inplace 为 true,则使用给定的 method 对所有 nan 值进行内插的 dataframe;否则为 none。
示例代码:用 dataframe.interpolate() 方法对 dataframe 中所有 nan 值进行内插
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3, none, 3],
'y': [4, none, 8, none, 3]})
print("dataframe:")
print(df)
filled_df = df.interpolate()
print("interploated 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
interploated dataframe:
x y
0 1.0 4.0
1 2.0 6.0
2 3.0 8.0
3 3.0 5.5
4 3.0 3.0
它使用 linear 插值方法对 dataframe 中的所有 nan 值进行内插。
该方法与 pandas.dataframe.fillna() 相比更加智能,后者使用一个固定的值来替换 dataframe. 中的所有 nan 值。
示例代码:dataframe.interpolate() 方法用 method 参数
我们也可以在 dataframe.interpolate() 函数中设置 method 参数值,用不同的插值技术对 dataframe 中的 nan 值进行插值。
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3, none, 3],
'y': [4, none, 8, none, 3]})
print("dataframe:")
print(df)
filled_df = df.interpolate(method='polynomial', order=2)
print("interploated 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
interploated dataframe:
x y
0 1.000000 4.000
1 2.000000 7.125
2 3.000000 8.000
3 3.368421 6.625
4 3.000000 3.000
该方法使用二阶多项式插值方法对 dataframe 中的所有 nan 值进行插值。
这里,order=2 是 polynomial 函数的关键字参数。
示例代码:pandas dataframe.interpolate() 方法使用 axis 参数沿 row 轴进行插值
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3, none, 3],
'y': [4, none, 8, none, 3]})
print("dataframe:")
print(df)
filled_df = df.interpolate(axis=1)
print("interploated 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
interploated dataframe:
x y
0 1.0 4.0
1 2.0 2.0
2 3.0 8.0
3 nan nan
4 3.0 3.0
这里,我们设置 axis=1,以沿行轴插值 nan 值。在第 2 行,nan 值被沿第 2 行线性内插替换。
但是,在第 4 行中,由于第 4 行中的两个值都是 nan,所以即使在内插后,nan 值仍然存在。
示例代码:dataframe.interpolate() 方法带 limit 参数
dataframe.interpolate() 方法中的 limit 参数限制了该方法所要填充的连续 nan 值的最大数量。
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3, none, 3],
'y': [4, none, none, none, 3]})
print("dataframe:")
print(df)
filled_df = df.interpolate( limit = 1)
print("interploated dataframe:")
print(filled_df)
输出:
dataframe:
x y
0 1.0 4.0
1 2.0 nan
2 3.0 nan
3 nan nan
4 3.0 3.0
interploated dataframe:
x y
0 1.0 4.00
1 2.0 3.75
2 3.0 nan
3 3.0 nan
4 3.0 3.00
在这里,当一列中的一个 nan 值从上到下被填满后,同一列中下一个连续的 nan 值将保持不变。
示例代码:dataframe.interpolate() 方法带 limit_direction 参数的方法
dataframe.interpolate() 方法中的 limit-direction 参数控制沿着特定轴的方向,在这个方向上进行数值插值。
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3, none, 3],
'y': [4, none, none, none, 3]})
print("dataframe:")
print(df)
filled_df = df.interpolate(limit_direction ='backward', limit = 1)
print("interploated dataframe:")
print(filled_df)
输出:
dataframe:
x y
0 1.0 4.0
1 2.0 nan
2 3.0 nan
3 nan nan
4 3.0 3.0
interploated dataframe:
x y
0 1.0 4.00
1 2.0 nan
2 3.0 nan
3 3.0 3.25
4 3.0 3.00
在这里,当一列中的 nan 从底部填入后,同一列中下一个连续的 nan 值将保持不变。
用 dataframe.interpolate() 方法对时间序列数据进行内插
import pandas as pd
dates=['april-10', 'april-11', 'april-12', 'april-13']
fruits=['apple', 'papaya', 'banana', 'mango']
prices=[3, none, 2, 4]
df = pd.dataframe({'date':dates ,
'fruit':fruits ,
'price': prices})
print(df)
df.interpolate(inplace=true)
print("interploated dataframe:")
print(df)
输出:
date fruit price
0 april-10 apple 3.0
1 april-11 papaya nan
2 april-12 banana 2.0
3 april-13 mango 4.0
interploated dataframe:
date fruit price
0 april-10 apple 3.0
1 april-11 papaya 2.5
2 april-12 banana 2.0
3 april-13 mango 4.0
由于 inplace=true,在调用 interpolate() 函数后,原 dataframe 被修改。
转载请发邮件至 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 系列日期时间转换为字符串

