John
John

Reputation: 3080

How to replace value of a file csv based on another csv file?

I have two csv files: 1.csv and 2.csv.

1.csv

id,name
a1, ABC
b2, CDF
x3,GIK

2.csv

id,name
x3,XYZ
a1,MNK
b2,NNN

I want to make the id field of 2.csv same as the id field in the 1.csv and change the name order corresponding to the id also. So the output of 2.csv will be

id,name
a1, MNK
b2, NNN
x3,XYZ

I am doing as follows but the process is too slow because it using two loop. How to speed up it? Thanks

df_1 = pd.read_csv('1.csv')
df_2 = pd.read_csv('2.csv')

for index_1, row_1 in df_1.iterrows():
    for index_2, row_2 in dr_2.iterrows():
        if (row_1['id']==row_2['id']):
            print(row_1['id'], row_2['name'])
            continue;

Upvotes: 0

Views: 242

Answers (1)

Kai Aeberli
Kai Aeberli

Reputation: 1220

Try this:

import pandas as pd
df_1 = pd.read_csv('1.csv')
df_2 = pd.read_csv('2.csv')
df_2 = df_2.set_index("id")
df_2 = df_2.reindex(index=df_1.id)
df_2.to_csv('2_reordered.csv')

Upvotes: 1

Related Questions