pandas - 使用聚合函数填充 pandas groupby 中缺失的日期

标签 pandas group-by aggregate-functions

我有一个数据框,其中每个 id 可能有间隙。我想在使用聚合函数之前填充每个组中缺失的日期(带有 NaN 值),因为它们似乎不按日期排列,但我收到以下错误。

import pandas as pd

df = pd.DataFrame({'date': ['2014-08-01',
                            '2014-08-02', 
                            '2014-08-03',
                            '2014-08-04',
                            '2014-08-10',
                            '2014-08-15',
                            '2014-08-12',
                            '2014-08-15',
                            '2014-08-20',
                            '2014-08-21',
                            '2014-08-22',                            
                            '2014-08-30',                            
                           ], 
                   'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 

                   'id': ['a', 'a', 'a', 'a', 'a', 'a',  'b', 'b', 'b', 'b', 'b', 'b']})

df['date'] = pd.to_datetime(df['date'])

df.sort_values('date', ascending=True).groupby('id').set_index('date').resample('1D').rolling(2).mean()

AttributeError: 'DataFrameGroupBy' object has no attribute 'set_index'

最佳答案

IIUC使用Series.asfreq添加连续的日期时间:

df['date'] = pd.to_datetime(df['date'])

df1 = (df.sort_values('date', ascending=True)
        .set_index('date')
        .groupby('id')
        .apply(lambda x: x.asfreq('d').rolling(2).mean())

print (df1)

               value
id date             
a  2014-08-01    NaN
   2014-08-02    1.5
   2014-08-03    2.5
   2014-08-04    3.5
   2014-08-05    NaN
   2014-08-06    NaN
   2014-08-07    NaN
   2014-08-08    NaN
   2014-08-09    NaN
   2014-08-10    NaN
   2014-08-11    NaN
   2014-08-12    NaN
   2014-08-13    NaN
   2014-08-14    NaN
   2014-08-15    NaN
b  2014-08-12    NaN
   2014-08-13    NaN
   2014-08-14    NaN
   2014-08-15    NaN
   2014-08-16    NaN
   2014-08-17    NaN
   2014-08-18    NaN
   2014-08-19    NaN
   2014-08-20    NaN
   2014-08-21    9.5
   2014-08-22   10.5
   2014-08-23    NaN
   2014-08-24    NaN
   2014-08-25    NaN
   2014-08-26    NaN
   2014-08-27    NaN
   2014-08-28    NaN
   2014-08-29    NaN
   2014-08-30    NaN

df['date'] = pd.to_datetime(df['date'])

df2 = (df.sort_values('date', ascending=True)
        .set_index('date')
        .groupby('id')['value']
        .apply(lambda x: x.asfreq('d').rolling(2, min_periods=1).mean()))

print (df2)
id  date      
a   2014-08-01     1.0
    2014-08-02     1.5
    2014-08-03     2.5
    2014-08-04     3.5
    2014-08-05     4.0
    2014-08-06     NaN
    2014-08-07     NaN
    2014-08-08     NaN
    2014-08-09     NaN
    2014-08-10     5.0
    2014-08-11     5.0
    2014-08-12     NaN
    2014-08-13     NaN
    2014-08-14     NaN
    2014-08-15     6.0
b   2014-08-12     7.0
    2014-08-13     7.0
    2014-08-14     NaN
    2014-08-15     8.0
    2014-08-16     8.0
    2014-08-17     NaN
    2014-08-18     NaN
    2014-08-19     NaN
    2014-08-20     9.0
    2014-08-21     9.5
    2014-08-22    10.5
    2014-08-23    11.0
    2014-08-24     NaN
    2014-08-25     NaN
    2014-08-26     NaN
    2014-08-27     NaN
    2014-08-28     NaN
    2014-08-29     NaN
    2014-08-30    12.0
Name: value, dtype: float64

如果需要前向填充缺失值:

df['date'] = pd.to_datetime(df['date'])

s = (df.sort_values('date', ascending=True)
        .set_index('date')
        .groupby('id')['value']
        .apply(lambda x: x.asfreq('d', method='ffill')))

print (s)
id  date      
a   2014-08-01     1
    2014-08-02     2
    2014-08-03     3
    2014-08-04     4
    2014-08-05     4
    2014-08-06     4
    2014-08-07     4
    2014-08-08     4
    2014-08-09     4
    2014-08-10     5
    2014-08-11     5
    2014-08-12     5
    2014-08-13     5
    2014-08-14     5
    2014-08-15     6
b   2014-08-12     7
    2014-08-13     7
    2014-08-14     7
    2014-08-15     8
    2014-08-16     8
    2014-08-17     8
    2014-08-18     8
    2014-08-19     8
    2014-08-20     9
    2014-08-21    10
    2014-08-22    11
    2014-08-23    11
    2014-08-24    11
    2014-08-25    11
    2014-08-26    11
    2014-08-27    11
    2014-08-28    11
    2014-08-29    11
    2014-08-30    12
Name: value, dtype: int64

关于pandas - 使用聚合函数填充 pandas groupby 中缺失的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76235492/

相关文章:

python - 使用 matplotlib/seaborn 绘制数据透视表中的子图

python - Pandas 重新映射到列中的范围

python - 基于增长率优化值的迭代计算

mysql - Group by 不能与 mysql 子句中的 where 一起使用?

如果 MAX() 等于当前行 SUM() 聚合值,MySQL 如何选择 1?

sql - 在 Where 子句中使用 MySQL Select 语句中的变量

python - pandas 比 ETL 的 csv 模块更高效吗

python - 选择 Pandas 多索引数据框中只有 1 个条目的元素

mysql - 如何在 MySQL 中使用 group by 获取跨组的高值和低值?

sql - 为什么 SUM(COL1 + COL2) 和 SUM(COL1) + SUM(COL2) 会产生不同的结果?