pandas 熊猫在 Groupby 中重新索引日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32275540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:49:43  来源:igfitidea点击:

Pandas reindex dates in Groupby

pythonpandasreindex

提问by clg4

I have a dataframe with sporadic dates as the index, and columns = 'id' and 'num'. I would like to pd.groupbythe 'id' column, and apply the reindex to each group in the dataframe.

我有一个以零星日期为索引的数据框,列 = 'id' 和 'num'。我想要pd.groupby“id”列,并将重新索引应用于数据框中的每个组。

My sample dataset looks like this:

我的示例数据集如下所示:

            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

My expected output once pd.reindexwith ffillis:

我预计,一旦输出pd.reindexffill的是:

            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

I have tried this, among other things to no avail: newdf=df.groupby('id').reindex(method='ffill')Which returns error:AttributeError: Cannot access callable attribute 'reindex' of 'DataFrameGroupBy' objects, try using the 'apply' method

我试过这个,除此之外无济于事: newdf=df.groupby('id').reindex(method='ffill')返回错误:AttributeError: Cannot access callable attribute 'reindex' of 'DataFrameGroupBy' objects, try using the 'apply' method

Any help would be much appreciated

任何帮助将非常感激

回答by JoeCondron

There's probably a slicker way to do this but this works:

可能有一种更巧妙的方法来做到这一点,但这有效:

def reindex_by_date(df):
    dates = pd.date_range(df.index.min(), df.index.max())
    return df.reindex(dates).ffill()

df.groupby('id').apply(reindex_by_date).reset_index(0, drop=True)