我有一个以零星日期为索引的数据框,列为'id'和'num'。我想pd.groupby
“id”列,并对数据帧中的每个组应用reindex。
我的示例数据集如下所示:
id num
2015-08-01 1 3
2015-08-05 1 5
2015-08-06 1 4
2015-07-31 2 1
2015-08-03 2 2
2015-08-06 2 3
使用ffill
时,我的预期输出是:
id num
2015-08-01 1 3
2015-08-02 1 3
2015-08-03 1 3
2015-08-04 1 3
2015-08-05 1 5
2015-08-06 1 4
2015-07-31 2 1
2015-08-01 2 1
2015-08-02 2 1
2015-08-03 2 2
2015-08-04 2 2
2015-08-05 2 2
2015-08-06 2 3
我试过这样做,但没有成功:
newdf=df.groupby('id').reindex(method='ffill')
返回错误:AttributeError: Cannot access callable attribute 'reindex' of 'DataFrameGroupBy' objects, try using the 'apply' method
任何帮助都将不胜感激
可能有一种更巧妙的方法可以做到这一点,但这是有效的:
相关问题 更多 >
编程相关推荐