简体   繁体   English

Python Pandas Inner加入2个具有不同标题的csv文件

[英]Python pandas Inner join 2 csv files with different header

I did some research but cant find the answer for my case I have 2 csv files 我做了一些研究,但找不到适合我的案例的答案,我有2个CSV文件

a.csv CSV

id, fname, lname, address
1, aaa, bbb, ccc
2, abb, ccb, ddd
3, ddd, eee, fff
4, eee, ggg, fff
5, EEE, GGG, RRRR

and 2nd file 和第二个文件

b.csv b.csv

ID
1
3
5

desire result would be (the id header below is not strict but id would be fine) 期望的结果是(下面的id标头并不严格,但id可以)

id, fname, lname, address
1, aaa, bbb, ccc
3, ddd, eee, fff
5, EEE, GGG, RRRR

what i tried 我尝试过的

merged = a.merge(b, left_on = ['id'],
                right_on= ['ID'],
                how = 'inner')
merged.to_csv(r'C:\things\output.csv', index=False)

and got a cannot allocate memory for array error... 并无法为数组错误分配内存...

edit: 编辑:

the code below would work fine if headers are exactly the same (both are 'id')but life is not perfect 如果标头完全相同(都为'id'),则下面的代码可以正常工作,但生活并不完美

merged = a.merge(b, on = 'id')

使用isin

merged=a.loc[a.id.isin(b.ID),:]

I think you should just be able to do 我想你应该能够做

merged = pd.merge(a, b, left_on = ['id'],
            right_on= ['ID'],
            how = 'inner').drop('ID', axis='columns')
merged.to_csv(r'C:\things\output.csv', index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM