Pandas DataFrame interpolate() 方法
定义和用法
interpolate()
方法根据指定的方法替换 NULL 值。
实例
用前一行和后一行之间的数字替换 NULL 值:
在此例中,我们使用名为 data.csv 的 .csv 文件。
import pandas as pd df = pd.read_csv('data.csv') newdf = df.interpolate(method='linear')
语法
dataframe.interpolate(method, axis, inplace, limit, limit_direction, limit_area, downcast, kwargs)
参数
参数是关键字参数。
参数 | 值 | 描述 |
---|---|---|
method |
|
可选。指定替换 NULL 值时使用的方法。 默认为 'linear'。 |
axis |
|
可选。沿其填充 NULL 值的轴。 默认为 0。 |
inplace |
|
可选。默认为 False。
|
limit |
|
可选。指定要填充的 NULL 值的最大数量(如果指定了方法)。 默认为 None。 |
limit_direction |
|
可选。指定填充的方向。 如果 method 是 backfill 或 bfill,则默认是 'backward'。 默认为 'forward'。 |
limit_area |
|
可选。指定填充的限制:
默认为 None。 |
downcast |
|
可选。用于填充特定数据类型的值的字典。 |
返回值
包含结果的 DataFrame,或者如果 inplace
参数设置为 True,则返回 None。