Pandas DataFrame interpolate() 方法
示例
使用上一行和下一行的数字替换 NULL 值
在此示例中,我们使用名为 data.csv 的 .csv 文件
import pandas as pd
df = pd.read_csv('data.csv')
newdf = df.interpolate(method='linear')
自己动手试一试 »
定义和用法
interpolate()
方法根据指定的方法替换 NULL 值。
语法
dataframe.interpolate(method, axis, inplace, limit, limit_direction, limit_area, downcast, kwargs)
参数
参数是 关键字参数。
参数 | 值 | 描述 |
---|---|---|
method | 'linear' |
可选,默认为 'linear'。指定替换 NULL 值时使用的方法 |
axis | 0 |
可选,默认 0。用于填充 NULL 值的轴。 |
inplace | True |
可选,默认 False。如果为 True:在当前 DataFrame 中进行替换。如果为 False:返回一个已进行替换的副本。 |
limit | 数字None |
可选,默认为 None。指定要填充的最大 NULL 值数量(如果指定了 method) |
limit_direction | 'forward' |
可选,默认为 'forward'(如果 method 是 backfill 或 bfill,则默认的 limit_direction 为 'backward')。指定填充的方向。 |
limit_area | 无 |
可选,默认为 None。指定填充的限制。 None - 无限制 'inside' - 仅填充有效值内部的 NULL 值 'outside' - 仅填充有效值外部的 NULL 值 |
downcast | 字典None |
可选,一个用于填充特定数据类型的值的字典。 |
返回值
一个带有结果的DataFrame,如果 inplace 参数设置为 True,则为 None。