I have 2 excel files, each containing 2 columns like below:
file1:
name price
a 1
b 78
f 54
g 32
t 2
file2:
name price
f 4
x 23
e 76
a 4
o 0
I want to read file1
and search for values where file2
name is equal to file1
name and extract prices, then write them in price column of file2
.
file2
should look like this:
file2:
name price
f 54
x 23
e 76
a 1
o 0
I have tried as below (I've saved the excel files as CSV):
import pandas as pd
import numpy as np
df1 = pd.DataFrame(pd.read_csv('file1.csv'))
df2 = pd.DataFrame(pd.read_csv('file2.csv'))
if(df1[name] in df2[name]):
df2[price] = df1[price]
df2.to_csv('file2.csv')
pd.read_csv
returns apd.DataFrame
object so no need to wrap it aroundpd.read_csv
see the duplicate post it will be really helpful now and in future