【发布时间】:2015-08-28 16:18:25
【问题描述】:
我有一个以零星日期为索引的数据框,列 = 'id' 和 'num'。我想pd.groupby 'id' 列,并将重新索引应用于数据框中的每个组。
我的示例数据集如下所示:
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
我对@987654323@ 和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
任何帮助将不胜感激
【问题讨论】: