[英]Combining certain columns from three excel files in one pandas DF based on the column number-Using python pandas
I have three excel files that was read to pandas DFs, the tables are below: DF1:我有三个 excel 文件被读取到 pandas DFs,表格如下:DF1:
Column A ![]() |
Column B ![]() |
---|---|
F ![]() |
G ![]() |
M![]() |
K![]() |
DF2: DF2:
Column A ![]() |
Column B ![]() |
---|---|
J![]() |
D![]() |
N![]() |
L![]() |
DF3:东风三号:
Column A ![]() |
Column B ![]() |
Column C ![]() |
---|---|---|
J![]() |
E![]() |
Y![]() |
N![]() |
Q![]() |
V ![]() |
J![]() |
D![]() |
B![]() |
E![]() |
B![]() |
F ![]() |
What I want to do is creating a fourth DF containing joined certain columns from the dataframes (Based on the column index) from the dataframes.我想要做的是创建第四个 DF,其中包含数据帧中连接的某些列(基于列索引)。 for DF4, what I want it to be: 1)(Column A from DF1) with (Column C from Df3) with (Column A from DF2)
对于 DF4,我希望它是:1)(DF1 的 A 列)和(Df3 的 C 列)和(DF2 的 A 列)
2)(Column B from DF1) with (Column A from Df3) with (Column B from DF2) 2)(DF1的B列)和(Df3的A列)和(DF2的B列)
The expected result of DF4: DF4的预期结果:
Char 1![]() |
CHAR2![]() |
---|---|
F ![]() |
G ![]() |
M![]() |
K![]() |
Y![]() |
J![]() |
V ![]() |
N![]() |
B![]() |
J![]() |
F ![]() |
E![]() |
J![]() |
D![]() |
N![]() |
L![]() |
what I wrote for now: `我现在写的是:`
import pandas as pd
NUR = pd.read_excel(r'C:\Users\jalal.hasain\Desktop\Copy of NUR Data 20-12.xlsx',
sheet_name=['2G','3G','4G'])
DF1=NUR.get('2G')
DF2=NUR.get('3G')
DF3=NUR.get('4G')
` `
Thanks in advance.提前致谢。
Use concat
with get columns in expected order in list for DF3
with set columns names by DataFrame.set_axis
:将
concat
与DF3
列表中预期顺序的获取列一起使用,并通过DataFrame.set_axis
设置列名称:
df = pd.concat([DF1,
DF3[['Column C','Column A']].set_axis(['Column A','Column B'], axis=1),
DF2], ignore_index=True)
print (df)
Column A Column B
0 F G
1 M K
2 Y J
3 V N
4 B J
5 F E
6 J D
7 N L
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.