Skip to content Skip to sidebar Skip to footer

Python Pandas: How To Update A Csv File From Another Csv File

We have two CSV files: a.csv and b.csv. a.csv has tree columns: label, item1, item2. b.csv has two columns: item1, item2. If item1 and item2 in a.csv also occurr in b.csv, that's a

Solution 1:

You could use loc and a boolean condition to mask your df (here representing a.csv) and set the label to 1 if that condition is met:

In [18]:

df.loc[(df['item1'] == df1['item1'])& (df['item2'] == df1['item2']), 'label'] = 1
df
Out[18]:
   label  item1  item2
0      0    123     35
1      0    342    721
2      1    876    243

If you want to set all row values you could use np.where:

In [19]:

np.where((df['item1'] == df1['item1'])& (df['item2'] == df1['item2']), 1, 0)
Out[19]:
array([0, 0, 1])
In [20]:

df['label'] = np.where((df['item1'] == df1['item1'])& (df['item2'] == df1['item2']), 1, 0)
df
Out[20]:
   label  item1  item2
0      0    123     35
1      0    342    721
2      1    876    243

Post a Comment for "Python Pandas: How To Update A Csv File From Another Csv File"