使用熊猫对多个映射货币列进行操作

时间:2019-11-10 16:16:13

标签: python pandas

我有两个数据帧,分别是df1和df2。我想对df2的“ Amount_Dollar”列执行操作。基本上,在df1中,我具有历史货币数据,并且我想对df2中的Currency和Amount_Dollar进行按日期的操作,以计算df2中New_Amount_Dollar列的值。

对于'货币'== [AUD,BWP]我们需要将Amount_Dollar乘以相应日期的相应货币值。

如果df1中没有任何货币,则不要对“ Amount_Dollar”执行任何操作(意味着取相同的值)

对于其他可用货币,我们需要将Amount_Dollar除以相应日期的相应货币值。

例如,在df2中,对于Date = '01 -01-2019',我有第一种货币作为AUD,因此我要计算这样的New_Amount_Dollar值

New_Amount_Dollar = Amount_Dollar * df1中的AUD值,即New_Amount_Dollar = 19298 * 98 = 1891204

另一个示例,在df2中,我以第三种货币作为COP的Date = '03 -01-2019,因此我要计算这样的New_Amount_Dollar值

New_Amount_Dollar = df1中的Amount_Dollar / COP值,即New_Amount_Dollar = 5000 / 0.043 = 116279.06

import pandas as pd
data1 = {'Date':['01-01-2019', '02-01-2019', '03-01-2019', 
                 '04-01-2019','05-01-2019'],
        'AUD':[98, 98.5, 99, 99.5, 97],
        'BWP':[30, 31, 33, 32, 31],
        'CAD':[0.02, 0.0192, 0.0196, 0.0196, 0.0192],
        'BND':[0.99, 0.952, 0.970, 0.980, 0.970],
        'COP':[0.05, 0.047, 0.043, 0.047, 0.045]}
df1 = pd.DataFrame(data1)

data2 = {'Date':['01-01-2019', '02-01-2019', '03-01-2019', '04-01-2019','05-01-2019'],
        'Currency':['AUD','AUD','COP','NZD','BND'],
        'Amount_Dollar':[19298, 19210, 5000, 200, 2300],
        'New_Amount_Dollar':[0,0,0,0,0]
        }
df2 = pd.DataFrame(data2) 

df1

         Date    AUD  BWP     CAD    BND    COP
0  01-01-2019   98.0   30  0.0200  0.990  0.050
1  03-01-2019   98.5   31  0.0192  0.952  0.047
2  04-01-2019   99.0   33  0.0196  0.970  0.043
3  05-01-2019   99.5   32  0.0196  0.980  0.047
4  06-01-2019   97.0   31  0.0192  0.970  0.045
5  09-01-2019  100.0   20  0.2000  0.230  0.023

df2

         Date Currency  Amount_Dollar  New_Amount_Dollar
0  01-01-2019      AUD          19298                  0
1  02-01-2019      AUD          19210                  0
2  03-01-2019      COP           5000                  0
3  04-01-2019      NZD            200                  0
4  07-01-2019      BND           2300                  0

预期结果

         Date Currency  Amount_Dollar  New_Amount_Dollar
0  01-01-2019      AUD          19298         1891204.00
1  02-01-2019      AUD          19210         1892185.00
2  03-01-2019      COP           5000          116279.06
3  04-01-2019      NZD            200             200.00
4  05-01-2019      BND           2300            2371.13

2 个答案:

答案 0 :(得分:1)

首先,将df1拆栈以获取所有FX的列,然后将其用于连接:

df1.set_index('Date', inplace=True)
df1 = pd.DataFrame(df1.unstack(), columns = ['FX'])

然后加入df2。为CAD和BWP添加蒙版,取CAD / BWP的倒数。然后按列进行操作以获取新的金额。

df2 = df2.merge(df1, left_on = ['Currency', 'Date'], right_index = True, how = 'left').fillna(1)
df2['mask'] = df2['Currency'].isin(['AUD', 'BWP'])
df2.loc[df2['mask'], 'FX'] = 1/df2.loc[df2['mask'], 'FX']
df2['New_Amount_Dollar'] = df2['Amount_Dollar'] / df2['FX']

答案 1 :(得分:1)

看到CAD是唯一不会改变其价值的货币,我猜您正在尝试将货币转换为CAD。但是,df1中的CAD汇率似乎有点古怪。

无论如何,这是代码:

# Format the data in `df1` to have the same general shape as `df2`
fx = pd.melt(df1, id_vars='Date', var_name='Currency', value_name='Rate')

# Exclude certain currencies from the conversion
exclude = ['CAD']
fx = fx[~fx.isin(exclude)]

# Some rates we multiply, some rates we divide
# Here, we convert the rate so the next step only involves multiplication
divide = ['COP']
fx['Rate'] = np.where(fx['Currency'].isin(divide), 1 / fx['Rate'], fx['Rate'])

# Perform the actual conversion
df2.merge(fx, how='left', on=['Date', 'Currency']) \
    .assign(New_Amount_Dollar=lambda df: df['Amount_Dollar'] * df['Rate'].fillna(1)) \
    .drop(columns='Rate')