热线电话:13121318867

登录
2019-01-27 阅读量: 885
根据第一列中的数据合并两个CSV文件

我有两个像我想要合并的csv文件 - 或多或少使用第一列ID_作为唯一标识符,并将AMT列附加到最终文件中的新行。

CSV1

ID_ CUSTOMER_ID_ EMAIL_ADDRESS_

1090 1 example1@example.com

1106 2 example2@example.com

1145 3 example3@example.com

1206 4 example4@example.com

1247 5 example5@example.com

1254 6 example6@example.com

1260 7 example7@example.com

1361 8 example8@example.com

1376 9 example9@example.com

CSV2

ID_ AMT

1090 5

1106 5

1145 5

1206 5

1247 5

1254 65

1260 5

1361 10

1376 5

解决办法:这可以使用Pandas库轻松完成。这是我的代码:

'''

This program reads two csv files and merges them based on a common key column.

'''

import pandas as pd

df1 = pd.read_csv('CSV1.csv')

df2 = pd.read_csv('CSV2.csv')

df3 = pd.merge(df1, df2, on = 'ID_')

df3.set_index('ID_', inplace = True)

df3.to_csv('CSV3.csv')

0.0000
5
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子