Df标头:在月末插入整整一年的标头行,并用零填充未填充的月份

时间:2019-05-23 04:48:35

标签: python pandas dataframe

下午所有时间

截至2019年3月30日的测试数据:

Test_Data = [
                ('Index', ['Year_Month','Done_RFQ','Not_Done_RFQ','Total_RFQ']),
                ('0', ['2019-01',10,20,30]),
                ('1', ['2019-02', 10, 20, 30]),
                ('2', ['2019-03', 20, 40, 60]),
             ]

df = pd.DataFrame(dict(Test_Data))
print(df)

          Index        0        1        2
0    Year_Month  2019-01  2019-02  2019-03
1      Done_RFQ       10       10       20
2  Not_Done_RFQ       20       20       40
3     Total_RFQ       30       30       60

截至2019年3月31日的预期产量

enter image description here

截至2019年4月30日的预期产量

enter image description here

随着每个月的进行,未格式化的df会增加一列数据

我想:

a。替换现有df中的标题,请注意,三月只有四列,然后四月只有5列。...12月为13:

df.columns = ['Report_Mongo','Month_1','Month_2','Month_3','Month_4','Month_5','Month_6','Month_7','Month_8','Month_9','Month_10','Month_11','Month_12']

b。随着一年的过去,零值将被数据取代。面临的挑战是确定已经过去了几个月,并且仅使用数据更新未填充的列

1 个答案:

答案 0 :(得分:1)

您可以按原始列的长度和DataFrame.reindex分配列:

c = ['Report_Mongo','Month_1','Month_2','Month_3','Month_4','Month_5','Month_6',
     'Month_7','Month_8','Month_9','Month_10','Month_11','Month_12']

df.columns = c[:len(df.columns)]
df = df.reindex(c, axis=1, fill_value=0)
print (df)
   Report_Mongo  Month_1  Month_2  Month_3  Month_4  Month_5  Month_6  \
0    Year_Month  2019-01  2019-02  2019-03        0        0        0   
1      Done_RFQ       10       10       20        0        0        0   
2  Not_Done_RFQ       20       20       40        0        0        0   
3     Total_RFQ       30       30       60        0        0        0   

   Month_7  Month_8  Month_9  Month_10  Month_11  Month_12  
0        0        0        0         0         0         0  
1        0        0        0         0         0         0  
2        0        0        0         0         0         0  
3        0        0        0         0         0         0  

替代方法是创建带有月度期间的标头,优点是所有行中的数字数据均仅

#set columns by first row
df.columns = df.iloc[0]
#remove first row and create index by first column
df = df.iloc[1:].set_index('Year_Month')
#convert columns to month periods 
df.columns = pd.to_datetime(df.columns).to_period('m')
#reindex to full year
df = df.reindex(pd.period_range(start='2019-01',end='2019-12',freq='m'),axis=1,fill_value=0)
print (df)
             2019-01 2019-02 2019-03  2019-04  2019-05  2019-06  2019-07  \
Year_Month                                                                 
Done_RFQ          10      10      20        0        0        0        0   
Not_Done_RFQ      20      20      40        0        0        0        0   
Total_RFQ         30      30      60        0        0        0        0   

              2019-08  2019-09  2019-10  2019-11  2019-12  
Year_Month                                                 
Done_RFQ            0        0        0        0        0  
Not_Done_RFQ        0        0        0        0        0  
Total_RFQ           0        0        0        0        0