在Python的同一列中用下一个值填充NaN值

时间:2020-10-29 01:33:31

标签: python pandas dataframe na

我想用NaN的列number中的下一个值填充df值:

        Id  Date        is_start        number
151256  30  2010-09-21  False           NaN
237558  30  2010-09-22  False           0.0
36922   120 2010-10-13  False           0.0
246284  80  2010-09-21  False           NaN
47655   80  2010-09-21  False           658.0

可复制的示例:

import pandas as pd
import numpy as np
import datetime

sample_df = pd.DataFrame({'Id': {151256: 30, 237558: 30, 36922: 120, 246284: 80, 47655: 80},
 'Date': {151256: datetime.date(2010, 9, 21),
  237558: datetime.date(2010, 9, 22),
  36922: datetime.date(2010, 10, 13),
  246284: datetime.date(2010, 9, 21),
  47655: datetime.date(2010, 9, 21)},
 'is_start': {151256: False,
  237558: False,
  36922: False,
  246284: False,
  47655: False},
 'number': {151256: np.nan,
  237558: 0.0,
  36922: 0.0,
  246284: np.nan,
  47655: 658.0}})
sample_df

预期输出:

        Id  Date        is_start        number
151256  30  2010-09-21  False           0.0   (replaced)
237558  30  2010-09-22  False           0.0
36922   120 2010-10-13  False           0.0
246284  80  2010-09-21  False           658.0 (replaced)
47655   80  2010-09-21  False           658.0

我尝试过:

sample_df['number'] = sample_df.fillna(sample_df.number.shift())

但得到了输出:

        Id  Date    is_start    number
151256  30  2010-09-21  False   30
237558  30  2010-09-22  False   30
36922   120 2010-10-13  False   120
246284  80  2010-09-21  False   80
47655   80  2010-09-21  False   80

number代表Id列中的值。为什么会这样,正确的方法是什么?

2 个答案:

答案 0 :(得分:1)

在这里检查bfill通知限制是仅填充下一个NaN值

df.number = df.number.bfill(limit=1)
Out[138]: 
151256      0.0
237558      0.0
36922       0.0
246284    658.0
47655     658.0
Name: number, dtype: float64

答案 1 :(得分:1)

BEN_YO的解决方案就是答案,但这是fillnashift(-1)的替代方案:

sample_df['number'] = sample_df['number'].fillna(sample_df['number'].shift(-1))
sample_df
Out[1]: 
         Id        Date  is_start  number
151256   30  2010-09-21     False     0.0
237558   30  2010-09-22     False     0.0
36922   120  2010-10-13     False     0.0
246284   80  2010-09-21     False   658.0
47655    80  2010-09-21     False   658.0