【问题标题】:PANDAS (poputating datetime and ffill() the data in dataframe in pandas)PANDAS(填充日期时间和 ffill() 大熊猫数据框中的数据)
【发布时间】:2020-09-23 01:44:37
【问题描述】:

我有一个如下的数据框,它只有工作日数据(即 周末除外)

effective_date,ent_id 
2020-02-03,349114.0
2020-02-03,1559910.0
2020-02-03,23431626.0
2020-02-03,15747736.0
2020-02-04,21349114.0
2020-02-04,15559910.0
2020-02-04,323431626.0
2020-02-04,5747736.0
2020-02-05,76349114.0
2020-02-05,5459910.0
2020-02-05,89431626.0 
2020-02-05,37747736.0 
2020-02-06,10349114.0
2020-02-06,26559910.0
2020-02-06,35431626.0
2020-02-06,88747736.0
2020-02-07,34913414.0 
2020-02-07,15591910.0 
2020-02-07,234318626.0
2020-02-07,1574436.0
2020-02-10,139114.0 
2020-02-10,4359910.0
2020-02-10,43431626.0
2020-02-10,10947736.0

我必须对此数据帧执行两个操作 1) 填充 缺少时间序列,即 2020-02-08 和 2020-02-09 2) 是转发 填充前一个日期的整个数据块

所以我的输出数据框应该是这样的

effective_date,ent_id
> 2020-02-03,349114.0 2020-02-03,1559910.0 2020-02-03,23431626.0
> 2020-02-03,15747736.0 2020-02-04,21349114.0 2020-02-04,15559910.0
> 2020-02-04,323431626.0 2020-02-04,5747736.0 2020-02-05,76349114.0
> 2020-02-05,5459910.0 2020-02-05,89431626.0 2020-02-05,37747736.0
> 2020-02-06,10349114.0 2020-02-06,26559910.0 2020-02-06,35431626.0
> 2020-02-06,88747736.0 2020-02-07,34913414.0 2020-02-07,15591910.0
> 2020-02-07,234318626.0 2020-02-07,1574436.0 2020-02-08,34913414.0
> 2020-02-08,15591910.0 2020-02-08,234318626.0 2020-02-08,1574436.0
> 2020-02-09,34913414.0 2020-02-09,15591910.0 2020-02-09,234318626.0
> 2020-02-09,1574436.0 2020-02-10,139114.0 2020-02-10,4359910.0
> 2020-02-10,43431626.0 2020-02-10,10947736.0

【问题讨论】:

    标签: python pandas date time-series


    【解决方案1】:

    使用GroupBy.cumcount 作为计数器,由DataFrame.set_index 用于MultiIndex,由DataFrame.unstack 重新整形,由DataFrame.asfreq 添加所有缺失的日期时间,然后用DataFrame.stack 重新整形,首先删除计数器级别DataFrame.reset_index 然后将 effective_date 转换为列:

    df['effective_date'] = pd.to_datetime(df['effective_date'])
    
    df1 = (df.set_index(['effective_date',df.groupby('effective_date').cumcount()])
             .unstack()
             .asfreq('D', method='ffill')
             .stack()
             .reset_index(level=1, drop=True)
             .reset_index())
    

    print (df1)
       effective_date       ent_id
    0      2020-02-03     349114.0
    1      2020-02-03    1559910.0
    2      2020-02-03   23431626.0
    3      2020-02-03   15747736.0
    4      2020-02-04   21349114.0
    5      2020-02-04   15559910.0
    6      2020-02-04  323431626.0
    7      2020-02-04    5747736.0
    8      2020-02-05   76349114.0
    9      2020-02-05    5459910.0
    10     2020-02-05   89431626.0
    11     2020-02-05   37747736.0
    12     2020-02-06   10349114.0
    13     2020-02-06   26559910.0
    14     2020-02-06   35431626.0
    15     2020-02-06   88747736.0
    16     2020-02-07   34913414.0
    17     2020-02-07   15591910.0
    18     2020-02-07  234318626.0
    19     2020-02-07    1574436.0
    20     2020-02-08   34913414.0
    21     2020-02-08   15591910.0
    22     2020-02-08  234318626.0
    23     2020-02-08    1574436.0
    24     2020-02-09   34913414.0
    25     2020-02-09   15591910.0
    26     2020-02-09  234318626.0
    27     2020-02-09    1574436.0
    28     2020-02-10     139114.0
    29     2020-02-10    4359910.0
    30     2020-02-10   43431626.0
    31     2020-02-10   10947736.0
    

    【讨论】:

    猜你喜欢
    • 2018-12-19
    • 2022-11-02
    • 2020-03-30
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    相关资源
    最近更新 更多