【问题标题】:creating and filling empty dates with zeroes创建并用零填充空日期
【发布时间】:2022-01-26 22:02:33
【问题描述】:

我有一个数据框df

df=pd.read_csv('https://raw.githubusercontent.com/amanaroratc/hello-world/master/x_restock.csv')
df

我想用restocking_events=0 填充每个Product_ID 的缺失日期。首先,我使用 dfdate=pd.DataFrame({'Date':pd.date_range(simple.Date.min(), simple.Date.max())}) 创建了一个 date_range 数据框,其中 simple 是一些主数据框,最小和最大日期是“2021-11-13”和“2021-11-30”。

【问题讨论】:

    标签: python pandas dataframe group-by


    【解决方案1】:

    用途:

    #added parse_dates for datetimes
    df=pd.read_csv('https://raw.githubusercontent.com/amanaroratc/hello-world/master/x_restock.csv', 
                   parse_dates=['Date'])
    

    第一个解决方案是在DataFrame.reindex 中添加从最小和最大日期时间到MultiIndex.from_product 的完整日期时间范围:

    mux = pd.MultiIndex.from_product([df['Product_ID'].unique(),
                                      pd.date_range(df.Date.min(), df.Date.max())], 
                                     names=['Product_ID','Dates'])
                                      
    df1 = df.set_index(['Product_ID','Date']).reindex(mux, fill_value=0).reset_index()
    print (df1)
          Product_ID      Dates  restocking_events
    0        1004746 2021-11-13                  0
    1        1004746 2021-11-14                  0
    2        1004746 2021-11-15                  0
    3        1004746 2021-11-16                  1
    4        1004746 2021-11-17                  0
             ...        ...                ...
    3379      976460 2021-11-26                  1
    3380      976460 2021-11-27                  0
    3381      976460 2021-11-28                  0
    3382      976460 2021-11-29                  0
    3383      976460 2021-11-30                  0
    
    [3384 rows x 3 columns]
    

    helper DataFrame 的另一个想法:

    from  itertools import product
    
    dfdate=pd.DataFrame(product(df['Product_ID'].unique(), 
                                pd.date_range(df.Date.min(), df.Date.max())),
                        columns=['Product_ID','Date'])
    print (dfdate)
          Product_ID       Date
    0        1004746 2021-11-13
    1        1004746 2021-11-14
    2        1004746 2021-11-15
    3        1004746 2021-11-16
    4        1004746 2021-11-17
             ...        ...
    3379      976460 2021-11-26
    3380      976460 2021-11-27
    3381      976460 2021-11-28
    3382      976460 2021-11-29
    3383      976460 2021-11-30
    
    [3384 rows x 2 columns]
    
    df = dfdate.merge(df, how='left').fillna({'restocking_events':0}, downcast='int')
    print (df)
          Product_ID       Date  restocking_events
    0        1004746 2021-11-13                  0
    1        1004746 2021-11-14                  0
    2        1004746 2021-11-15                  0
    3        1004746 2021-11-16                  1
    4        1004746 2021-11-17                  0
             ...        ...                ...
    3379      976460 2021-11-26                  1
    3380      976460 2021-11-27                  0
    3381      976460 2021-11-28                  0
    3382      976460 2021-11-29                  0
    3383      976460 2021-11-30                  0
    
    [3384 rows x 3 columns]
    

    或者如果需要每组连续的日期时间,请使用DataFrame.asfreq

    df2 = (df.set_index('Date')
             .groupby('Product_ID')['restocking_events']
             .apply(lambda x: x.asfreq('d', fill_value=0))
             .reset_index())
    print (df2)
          Product_ID       Date  restocking_events
    0         112714 2021-11-15                  1
    1         112714 2021-11-16                  1
    2         112714 2021-11-17                  0
    3         112714 2021-11-18                  1
    4         112714 2021-11-19                  0
             ...        ...                ...
    2209     3630918 2021-11-25                  0
    2210     3630918 2021-11-26                  0
    2211     3630918 2021-11-27                  0
    2212     3630918 2021-11-28                  0
    2213     3630918 2021-11-29                  1
    
    [2214 rows x 3 columns]
    

    【讨论】:

    • 在第二个选项中,restocking_events 仅在每个 Product_ID 最小值和最大值之间的某些日期填充为 0。对于所有 Product_ID 的 2021-11-13 和 2021-11-30 之间的所有缺失日期,我需要它们 =0
    • @AmanArora - 好的,然后使用第一个解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 2022-08-18
    • 2021-11-22
    相关资源
    最近更新 更多