大Pandas分组给日期重新编制索引

2024-05-15 08:02:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个以零星日期为索引的数据框,列为'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

任何帮助都将不胜感激


Tags: 数据id示例df错误nummethodpd