[英]How to Copy the Matching Columns between CSV Files Using Pandas?
I have two dataframes(f1_df and f2_df): 我有两个数据框(f1_df和f2_df):
f1_df looks like: f1_df看起来像:
ID,Name,Gender
1,Smith,M
2,John,M
f2_df looks like: f2_df看起来像:
name,gender,city,id
Problem: 问题:
I want the code to compare the header of f1_df with f2_df by itself and copy the data of the matching columns using panda. 我希望代码自己将f1_df的标头与f2_df进行比较,并使用panda复制匹配列的数据。
Output: 输出:
the output should be like this: 输出应该是这样的:
name,gender,city,id # name,gender,and id are the only matching columns btw f1_df and f2_df
Smith,M, ,1 # the data copied for name, gender, and id columns
John,M, ,2
I am new to Pandas and not sure how to handle the problem. 我是熊猫的新手,不确定如何处理该问题。 I have tried to do an inner join to the matching columns, but that did not work. 我试图对匹配的列进行内部联接,但这没有用。
Here is what I have so far: 这是我到目前为止的内容:
import pandas as pd
f1_df = pd.read_csv("file1.csv")
f2_df = pd.read_csv("file2.csv")
for i in f1_df:
for j in f2_df:
i = i.lower()
if i == j:
joined = f1_df.join(f2_df)
print joined
Any idea how to solve this? 任何想法如何解决这个问题?
try this if you want to merge / join your DFs on common columns: 如果要合并/加入公共列上的DF,请尝试以下方法:
first lets convert all columns to lower case: 首先让我们将所有列转换为小写:
df1.columns = df1.columns.str.lower()
df2.columns = df2.columns.str.lower()
now we can join on common columns 现在我们可以加入共同的专栏
common_cols = df2.columns.intersection(df1.columns).tolist()
joined = df1.set_index(common_cols).join(df2.set_index(common_cols)).reset_index()
Output: 输出:
In [259]: joined
Out[259]:
id name gender city
0 1 Smith M NaN
1 2 John M NaN
export to CSV: 导出为CSV:
In [262]: joined.to_csv('c:/temp/joined.csv', index=False)
c:/temp/joined.csv: c:/temp/joined.csv:
id,name,gender,city
1,Smith,M,
2,John,M,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.