Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Df Headers:在月末插入一整年的标题行,并用零填充未填充的月份_Python_Pandas_Dataframe - Fatal编程技术网

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

Python Df Headers:在月末插入一整年的标题行,并用零填充未填充的月份,python,pandas,dataframe,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,

下午好,

截至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日的预期产量

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

随着每个月的进展,未格式化的df将有一列额外的数据

我想:

a。替换现有df中的标题,注意在3月份只有四列,然后在4月份有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。随着这一年的进展,零价值将被数据所取代。挑战在于确定已经过去了多少个月,并且只使用数据更新未填充的列

您可以按原始列的长度分配列,并且:

另一种选择是创建带有月周期的标题,优点是所有行中只有数字数据:

#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  

多谢各位@jezrael@PeterLucas-不客气!又加了一句话,也许也有帮助。啊,太好了。再次感谢
#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