Python 如何合并多个图纸并使用图纸名称的名称重命名列名称?
我有以下数据。它全部位于一个excel文件中Python 如何合并多个图纸并使用图纸名称的名称重命名列名称?,python,excel,pandas,merge,concatenation,Python,Excel,Pandas,Merge,Concatenation,我有以下数据。它全部位于一个excel文件中 Sheet name: may2019 Productivity Count Date : 01-Apr-2020 00:00 to 30-Apr-2020 23:59 Date Type: Finalized Date Modality: All Name MR DX CT US MG BMD
Sheet name: may2019
Productivity Count
Date : 01-Apr-2020 00:00 to 30-Apr-2020 23:59
Date Type: Finalized Date Modality: All
Name MR DX CT US MG BMD TOTAL
Svetlana 29 275 101 126 5 5 541
Kate 32 652 67 171 1 0 923
Andrew 0 452 0 259 1 0 712
Tom 50 461 61 104 4 0 680
Maya 0 353 0 406 0 0 759
Ben 0 1009 0 143 0 0 1152
Justin 0 2 9 0 1 9 21
Total 111 3204 238 1209 12 14 4788
Sheet Name: June 2020
Productivity Count
Date : 01-Jun-2019 00:00 to 30-Jun-2019 23:59
Date Type: Finalized Date Modality: All
NAme US DX CT MR MG BMD TOTAL
Svetlana 4 0 17 6 0 4 31
Kate 158 526 64 48 1 0 797
Andrew 154 230 0 0 0 0 384
Tom 1 0 19 20 2 8 50
Maya 260 467 0 0 1 1 729
Ben 169 530 59 40 3 0 801
Justin 125 164 0 0 4 0 293
Alvin 0 1 0 0 0 0 1
Total 871 1918 159 114 11 13 3086
我想把所有的工作表合并到工作表上,删除所有工作表的前3行,这就是我要寻找的输出
Sl.No Name US_jun2019 DX_jun2019 CT_jun2019 MR_jun2019 MG_jun2019 BMD_jun2019 TOTAL_jun2019 MR_may2019 DX_may2019 CT_may2019 US_may2019 MG_may2019 BMD_may2019 TOTAL_may2019
1 Svetlana 4 0 17 6 0 4 31 29 275 101 126 5 5 541
2 Kate 158 526 64 48 1 0 797 32 652 67 171 1 0 923
3 Andrew 154 230 0 0 0 0 384 0 353 0 406 0 0 759
4 Tom 1 0 19 20 2 8 50 0 2 9 0 1 9 21
5 Maya 260 467 0 0 1 1 729 0 1009 0 143 0 0 1152
6 Ben 169 530 59 40 3 0 801 50 461 61 104 4 0 680
7 Justin 125 164 0 0 4 0 293 0 452 0 259 1 0 712
8 Alvin 0 1 0 0 0 0 1 #N/A #N/A #N/A #N/A #N/A #N/A #N/A
我尝试了下面的代码,但是输出不是我想要的
df=pd.concat(df,sort=False)
df= df.drop(df.index[[0,1]])
df=df.rename(columns=df.iloc[0])
df= df.drop(df.index[[0]])
df=df.drop(['Sl.No'], axis = 1)
print(df)
首先,阅读两份Excel表格 df1=pd.read\u excel('path/to/excel/file.xlsx',sheet\u name=“2019年5月”) >>>df2=pd.read\u excel('path/to/excel/file.xlsx',sheet\u name=“jun2019”) 放下前三行
>>> df1.drop(index=range(3), inplace=True)
>>> df2.drop(index=range(3), inplace=True)
将列重命名为第一行,然后删除第一行
df1.rename(columns=dict(zip(df1.columns,df1.iloc[0])),inplace=True)
>>>df1.drop(索引=[0],原地=真)
>>>重命名(columns=dict(zip(df2.columns,df2.iloc[0])),inplace=True)
>>>df2.drop(索引=[0],原地=真)
在列中添加后缀
>>df1.重命名(列=lambda col\u name:col\u name+''u may2019',inplace=True)
>>>df2.重命名(列=lambda col_name:col_name+''u jun2019',inplace=True)
删除第二个DF中的重复名称列
df2.drop(列=['Name'],inplace=True)
连接两个数据帧
df=pd.concat([df1,df2],轴=1,在位=True)
将所有代码放在一个位置:
将熊猫作为pd导入
df1=pd.read\u excel('path/to/excel/file.xlsx',sheet\u name=“2019年5月”)
df2=pd.read\u excel('path/to/excel/file.xlsx',sheet\u name=“jun2019”)
df1.drop(索引=范围(3),就地=真)
df2.下降(指数=范围(3),原地=真)
df1.rename(columns=dict(zip(df1.columns,df1.iloc[0])),inplace=True)
df1.drop(索引=[0],原地=真)
重命名(columns=dict(zip(df2.columns,df2.iloc[0])),inplace=True)
df2.drop(索引=[0],原地=真)
df1.重命名(列=lambda col_name:col_name+''2019年5月',inplace=True)
df2.重命名(列=lambda col_name:col_name+''u jun2019',inplace=True)
drop(列=['Name'],inplace=True)
df=pd.concat([df2,df1],轴=1,在位=True)
打印(df)
当然可以。如果有帮助,请接受并投票。