[英]Combining CSV's with Different Columns using Pandas [with key column]
I'm trying to combine two CSV files in Python, each CSV file has unique columns but both CSV files share a common key column. 我正在尝试在Python中合并两个CSV文件,每个CSV文件都有唯一的列,但是两个CSV文件共享一个公用的键列。
I've been looking around StackOverflow/Google/Pandas documentation but didn't find exactly what I was looking for. 我一直在寻找StackOverflow / Google / Pandas文档,但找不到我想要的东西。 The examples provided on the Pandas documentation pages for merge and concat are different from what I'm trying to achieve so I'm not sure if what I'm asking is possible with Pandas. Pandas文档页面上提供的有关merge和concat的示例与我要实现的示例不同,因此我不确定Pandas是否可以实现我要问的内容。
I've read in selected columns from both CSV files into separate dataframes, what I would like to do now is combine the two dataframes into a single dataframe based on the key column. 我已经将两个CSV文件中的选定列读入单独的数据框,现在我想做的就是根据键列将两个数据框组合成一个数据框。
Example
CSV 1:
Key Make Model
501 Audi A3
502 Audi A4
503 Audi A5
CSV 2:
Key Engine
501 2.0T
502 2.0T
503 2.0T
Combined Expected Result:
Key Make Model Engine
501 Audi A3 2.0T
502 Audi A4 2.0T
503 Audi A5 2.0T
You need to read your csvs into 2 separate data frames and then join them on 'Key' column. 您需要将csvs读入2个单独的数据帧,然后在“键”列中将它们加入。
import pandas as pd
df1 = pd.read_csv('csv1.csv')
df2 = pd.read_csv('csv2.csv')
df_final = df1.merge(df2, left_on = 'Key', right_on = 'Key')
Kacper Sobociński answer is correct, you can use pandas merge. KacperSobociński的回答是正确的,可以使用熊猫合并。
import pandas as pd
data1 = {'Key': [501,502,503],
'Make': ['Audi','Audi','Audi'],
'Model': ['A3','A4','A5']}
data2 = {'Key':[501,502,503],
'Engine': ['2.0T', '2.0T','2.0T']}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
df = pd.merge(df1,df2, how = 'inner', on = 'Key')
print(df)
Key Make Model Engine
0 501 Audi A3 2.0T
1 502 Audi A4 2.0T
2 503 Audi A5 2.0T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.