简体   繁体   中英

Python pandas - Divide a column in dataframe1 with a column in dataframe2 based on another column in dataframe1

Dateframe1

df = pd.DataFrame(SQL_Query, columns=[ X,Y . . . . Currency,Amount] 

Index         X             Y  ...         Currency          Amount
0             74            1  ...         USD               100
1             75            1  ...         EUR               5000
2             76            1  ...         AUD               300
3             79            1  ...         EUR               750

[1411137 rows x 162 columns]

A large SQL query so I avoid writing out all columns.

df1=pd.read_excel(r`FX_EUR.xlsx)

Index       Currency      FX
0             AUD      1.61350
1             BGN      1.95580
2             BRL      4.51450
3             CAD      1.45830
4             CHF      1.09280

So what would I like to achieve is to make a lookup in DF1 to see which Currency is used then divide the "DF1 Amount" column with "DF2 FX" column and to this for all rows in DF1. Either by making a third DF3 or by creating a new column i DF1 called Amount_EUR.

Any ideas on how to write this code?

You can use a map to apply the transformation -

import pandas as pd
df = pd.DataFrame({"Currency": ['USD', 'EUR', 'AUD', 'EUR'], "Amount": [100, 5000, 300, 750]})

df1 = pd.DataFrame({"Currency": ["AUD", "BGN", "BRL", "CAD", "EUR"], "FX": [1.6, 1.9, 4.5, 1.5, 1.1]})
df1 = df1.set_index("Currency")

df['FX'] = df['Currency'].map(df1.FX)
df['FX_Adj_Amt'] = df['Amount'].div(df['FX'])

df
#  Currency  Amount   Fx   FX_Adj_Amt
#0      USD     100  NaN          NaN
#1      EUR    5000  1.1  4545.454545
#2      AUD     300  1.6   187.500000
#3      EUR     750  1.1   681.818182

You could use merge to build a series containing the correct FX (same Currency ) with the same index as df . The division is then trivial:

fx = df.merge(df1, 'left', on='Currency')['FX']
df.loc[~ fx.isna(),'EUR_Amount'] = df.loc[~ fx.isna()]['Amount']/fx.loc[~ fx.isna()]

With your sample data it gives:

        X  Y  ... Currency  Amount  EUR_Amount
Index                                         
0      74  1  ...      USD     100         NaN
1      75  1  ...      EUR    5000         NaN
2      76  1  ...      AUD     300  185.931205
3      79  1  ...      EUR     750         NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM