Pandas DataFrame bfill() 方法
实例
将 NULL 替换为下一行中的值:
import pandas as pddf = pd.read_csv('data.csv')newdf = df.bfill()print(newdf.to_string())#注意:我们使用 to_string() 方法返回整个 DataFrame。#注意:17, 27, 91, 118, 141 这些行在 Calories 列上都是空值.
在上面的例子中,我们将使用名为 ‘data.csv’ 的 CSV 文件。
定义与用法
bfill() 方法将空值替换为下一行 (或下一列, 如果 axis 参数被设置成了 'columns').
语法
dataframe.bfill(axis, inplace, limit, downcast)
参数
axis, method,axis, inplace,limit, downcast 都是 关键字参数 。
| 参数 | Value | 描述 |
|---|---|---|
| axis | 0 1 'index' 'columns' | 可选, default 0。用于填充空值的轴 |
| inplace | True False | 可选, 默认值为 False。如果为 True:在当前 DataFrame 上完成替换。如果为 False:返回完成替换的副本 |
| limit | Number None | 可选, 默认 NULL。指定要填充的最大空值数 |
| downcast | Dictionary None | 可选, 为特定数据类型填充的字典值 |
返回值
一个 DataFrame 的结果,或如果 inplace 参数设置为 True,则为 None。