【问题标题】:How to set date index manually and fill zeros for missing rows in python panda dataframe如何手动设置日期索引并为python panda数据框中的缺失行填充零
【发布时间】:2021-01-13 05:20:16
【问题描述】:

我有一个下面给出的数据集,我有一个采用当前日期的参数:

product_name    serial_number     date           sum
"A"             "12"              "2020-01-01"   150        
"A"             "12"              "2020-01-02"   350
"A"             "12"              "2020-01-05"   550
"A"             "12"              "2020-01-10"   1500

例如,请将 current_date 设为“2020-01-15”。我正在尝试将索引从 current_date,“2020-01-15”手动设置为给定数据集(“2020-01-01”)中的最小日期,并将其输出为用零填充缺失日期的数据框:

product_name    serial_number     date           sum
    "A"             "12"          "2020-01-01"   150        
    "A"             "12"          "2020-01-02"   350
    "A"             "12"          "2020-01-03"   0
    "A"             "12"          "2020-01-04"   0 
    "A"             "12"          "2020-01-05"   550
    "A"             "12"          "2020-01-06"   0        
    "A"             "12"          "2020-01-07"   0
    "A"             "12"          "2020-01-08"   0
    "A"             "12"          "2020-01-09"   0 
    "A"             "12"          "2020-01-10"   1500 
    "A"             "12"          "2020-01-11"   0        
    "A"             "12"          "2020-01-12"   0
    "A"             "12"          "2020-01-13"   0
    "A"             "12"          "2020-01-14"   0 
    "A"             "12"          "2020-01-15"   0 

【问题讨论】:

    标签: python pandas dataframe time-series


    【解决方案1】:

    pivotDataFrame.reindex 一起使用date_rangeDataFrame.stack

    current_date = '2020-01-15'
    #if need dynamically set today
    #current_date = pd.to_datetime('today')
    r = pd.date_range(df['date'].min(), current_date, name='date')
    cols = ['product_name','serial_number']
    
    df = (df.pivot(cols, 'date', 'sum')
            .reindex(r, axis=1, fill_value=0)
            .stack()
            .reset_index(name='sum'))
       product_name  serial_number       date   sum
    0             A             12 2020-01-01   150
    1             A             12 2020-01-02   350
    2             A             12 2020-01-03     0
    3             A             12 2020-01-04     0
    4             A             12 2020-01-05   550
    5             A             12 2020-01-06     0
    6             A             12 2020-01-07     0
    7             A             12 2020-01-08     0
    8             A             12 2020-01-09     0
    9             A             12 2020-01-10  1500
    10            A             12 2020-01-11     0
    11            A             12 2020-01-12     0
    12            A             12 2020-01-13     0
    13            A             12 2020-01-14     0
    14            A             12 2020-01-15     0
    

    DataFrame.set_indexDataFrame.reindexMultiIndex.from_product

    current_date = '2020-01-15'
    #if need dynamically set today
    #current_date = pd.to_datetime('today')
    r = pd.date_range(df['date'].min(), current_date)
    mux = pd.MultiIndex.from_product([df['product_name'].unique(),
                                      df['serial_number'].unique(),
                                      r], names=['product_name','serial_number','date'])
    df = (df.set_index(['product_name','serial_number', 'date'])
            .reindex(mux, fill_value=0)
            .reset_index())
    

    对于更动态的解决方案,在列表理解中设置唯一值:

    current_date = '2020-01-15'
    #if need dynamically set today
    #current_date = pd.to_datetime('today')
    r = pd.date_range(df['date'].min(), current_date)
    cols = ['product_name','serial_number']
    
    uniq = [df[x].unique() for x in cols]
    mux = pd.MultiIndex.from_product(uniq+[r], names= cols + ['date'])
    df = (df.set_index(['product_name','serial_number', 'date'])
            .reindex(mux, fill_value=0)
            .reset_index())
    

    print (df)
     product_name  serial_number       date   sum
    0             A             12 2020-01-01   150
    1             A             12 2020-01-02   350
    2             A             12 2020-01-03     0
    3             A             12 2020-01-04     0
    4             A             12 2020-01-05   550
    5             A             12 2020-01-06     0
    6             A             12 2020-01-07     0
    7             A             12 2020-01-08     0
    8             A             12 2020-01-09     0
    9             A             12 2020-01-10  1500
    10            A             12 2020-01-11     0
    11            A             12 2020-01-12     0
    12            A             12 2020-01-13     0
    13            A             12 2020-01-14     0
    14            A             12 2020-01-15     0
    

    【讨论】:

    • 非常感谢您的回答,这是一个简单的例子。有什么方法可以在不传递列名的情况下将其应用于高维数据?
    • @user3104352 - 你能更具体一点吗? a high dimensional data 是什么意思?
    • 当然。在这种情况下,我们知道列,但除了“product_name”、“serial_name”和“date”之外,还有 100 个列。那么,我怎样才能为我提到的案例定制你的代码呢?非常感谢您。
    猜你喜欢
    • 2020-01-13
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2018-03-24
    • 2021-06-17
    • 2020-09-08
    • 2016-11-16
    • 2019-11-01
    相关资源
    最近更新 更多