pandas.core.groupby.DataFrameGroupBy.bfill#

DataFrameGroupBy.bfill(limit=None)[源代码]#

向后填充值。

Parameters:
<strong>limit</strong>int, optional

要填充的最大值数量。

Returns:
Series 或 DataFrame

填充了缺失值。

参见

Series.bfill

向后填充数据集中缺失的值。

DataFrame.bfill

向后填充数据集中缺失的值。

Series.fillna

填充 Series 的 NaN 值。

DataFrame.fillna

填充 DataFrame 的 NaN 值。

Examples

对于 Series:

>>> index = ['Falcon', 'Falcon', 'Parrot', 'Parrot', 'Parrot']
>>> s = pd.Series([None, 1, None, None, 3], index=index)
>>> s
Falcon    NaN
Falcon    1.0
Parrot    NaN
Parrot    NaN
Parrot    3.0
dtype: float64
>>> s.groupby(level=0).bfill()
Falcon    1.0
Falcon    1.0
Parrot    3.0
Parrot    3.0
Parrot    3.0
dtype: float64
>>> s.groupby(level=0).bfill(limit=1)
Falcon    1.0
Falcon    1.0
Parrot    NaN
Parrot    3.0
Parrot    3.0
dtype: float64

对于 DataFrame:

>>> df = pd.DataFrame({'A': [1, None, None, None, 4],
...                    'B': [None, None, 5, None, 7]}, index=index)
>>> df
          A         B
Falcon  1.0       NaN
Falcon  NaN       NaN
Parrot  NaN       5.0
Parrot  NaN       NaN
Parrot  4.0       7.0
>>> df.groupby(level=0).bfill()
          A         B
Falcon  1.0       NaN
Falcon  NaN       NaN
Parrot  4.0       5.0
Parrot  4.0       7.0
Parrot  4.0       7.0
>>> df.groupby(level=0).bfill(limit=1)
          A         B
Falcon  1.0       NaN
Falcon  NaN       NaN
Parrot  NaN       5.0
Parrot  4.0       7.0
Parrot  4.0       7.0