我想用NaN列中的下一个值填充值:numberdf
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列中的值。为什么会发生这种情况以及正确的方法是什么?
一只名叫tom的猫
至尊宝的传说
相关分类