Pandas reindex dates in Groupby

后端 未结 1 1980
我在风中等你
我在风中等你 2020-12-03 08:42

I have a dataframe with sporadic dates as the index, and columns = \'id\' and \'num\'. I would like to pd.groupby the \'id\' column, and apply the reindex to e

相关标签:
1条回答
  • 2020-12-03 08:52

    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)
    
    0 讨论(0)
提交回复
热议问题