In Python, how can I map column value in Table 1 to another Table 2 and append back to Table 1?

38
December 03, 2020, at 10:00 AM

I have a table with a list of currencies as column in df1['Ccy]

   Ccy    
0   GBP           
1   GBP        
2   USD       
3   EUR
4   GBP
5   USD

I have a second table that has values, df2:

    Ccy FX Rate
0   USD 0.750244
1   JPY 0.007196
2   GBP 1.000000
3   EUR 0.893390

How can I create a mapping as a new column in df1 that has the FX Rate column values per respective currency, e.g:

  Ccy       FX Rate
0   GBP     1.000000
1   GBP     1.000000   
2   USD     0.750244  
3   EUR     0.893390
4   GBP     1.000000   
5   USD     0.750244

I could do a mapping like the below but it replaces original currencies rather than creates a new column with the mapped numerical values: rename_dict = df2.set_index('Ccy').to_dict()['FX Rate'] df1 = df1.replace(rename_dict)

Want to simply add the mappings as a new column to the original df1.

Thanks!

Answer 1

You can merge two dataframes on "Ccy".

newdf = pd.merge(df1,df2,on="Ccy",how="left")
READ ALSO
How to write custom middleware and pass to socket-io custom event pipeline?

How to write custom middleware and pass to socket-io custom event pipeline?

I am using socket-io for building a chat appAnd I have many custom events and for each event I want to pass different middleware function? In my case io

41
TYPO3 on XAMPP: Changes in backend not/unreliably persisted

TYPO3 on XAMPP: Changes in backend not/unreliably persisted

I've a running TYPO3 project on a Linux webserver and now I'm trying to get it working properly on a local Windows 10 machine with XAMPP for development purposes

65
DBConnection error and include_once error

DBConnection error and include_once error

Now i hope this topic stays here,

40
Arranging rectangles in a circle with equal distance

Arranging rectangles in a circle with equal distance

I'm writing a program that renders mind mapsSo far, I've succeeded in drawing the root node and the first level of child nodes around it, in a circle

26