user9822426
user9822426

Reputation:

Need to compare two columns from two dataframes in pandas

I need to compare a first column value between two dataframes and change value of second column by multiplying the value with second column of second dataframe

dataframe1

Money   Currency
 34       USD
 31       EUR
 20       JPY

dataframe2

Curr   Value
EUR     1.21
JPY     0.78

my expected output

 Money            Currency
  34                 USD
  37.51(31*1.21)     USD
  15.6(20*0.78)      USD

Thanks in advance

Upvotes: 0

Views: 70

Answers (1)

jezrael
jezrael

Reputation: 862406

Use Series.map with second DataFrame and multiple by Series.mul (there is fill_value for multiple by 1 if no match value like for USD in sample data, so is created missing value):

s = dataframe2.set_index('Curr')['Value']
dataframe1['Money'] = dataframe1['Currency'].map(s).mul(dataframe1['Money'], fill_value=1)

dataframe1['Currency'] = 'USD'

print (dataframe1)
   Money Currency
0  34.00      USD
1  37.51      USD
2  15.60      USD

Detail:

print (dataframe1['Currency'].map(s))
0     NaN
1    1.21
2    0.78
Name: Currency, dtype: float64

Upvotes: 1

Related Questions