【问题标题】:Columnwise operation on multiple mapped columns using pandas使用 pandas 对多个映射列进行按列操作
【发布时间】:2019-11-07 06:20:17
【问题描述】:

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

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

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

例如,在 df2 中,我的第一个货币为澳元,日期 = '01-01-2019',所以我想计算 New_Amount_Dollar 值这样

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

另一个例子,在 df2 中我有第三种货币作为日期 = '03-01-2019 的 COP,所以我想计算 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','CAD','BND'],
        'Amount_Dollar':[19298, 19210, 5000, 200, 2300],
        'New_Amount_Dollar':[0,0,0,0,0]
        }
df2 = pd.DataFrame(data2) 
print (df2)

df1

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

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      CAD            200                  0
4  05-01-2019      BND           2300                  0

预期结果

         Date Currency  Amount_Dollar  New_Amount_Dollar
0  01-01-2019      AUD          19298            1891204
1  02-01-2019      AUD          19210          1892185.0
2  03-01-2019      COP           5000          116279.06
3  04-01-2019      CAD            200           10204.08
4  05-01-2019      BND           2300            2371.13

【问题讨论】:

  • 如何判断何时将 df1 中的值与 d2 相乘或相除?即如何区分乘以澳元和除以加元?只要它小于 1 除数?
  • 否 可以是任何不小于 1 的值。
  • 如果货币是 AUD 或 BWP,则乘以货币价值,否则除以

标签: python pandas


【解决方案1】:

你想要lookupisin()

# this is to know where to multiply
# where to divide
s = df2['Currency'].isin(['AUD', 'BWP'])

# the values to multiply/divide
m = df1.set_index('Date').lookup(df2['Date'],df2['Currency'])

df2['New_Amount_Dollar'] = df2['Amount_Dollar'] * np.where(s, m, 1/m)

输出:

         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.07
3  04-01-2019      CAD            200           10204.08
4  05-01-2019      BND           2300            2371.13

【讨论】:

  • 如果df1中缺少任何货币并且我想在“New_Amount_Dollar”中取相同的“Amount_Dollar”怎么办。
【解决方案2】:

尝试使用meltmerge

df_out = df2.merge(df1.melt('Date', var_name='Currency'), on= ['Date','Currency'])

df_out['New_Amount_Dollar'] = (df_out['Amount_Dollar'] * 
                               np.where(df_out['Currency'].isin(['AUD', 'BWP']),
                                        df_out['value'], 
                                        1/df_out['value']))
print(df_out)

输出:

         Date Currency  Amount_Dollar  New_Amount_Dollar  value
0  01-01-2019      AUD          19298        1891204.000 98.000
1  02-01-2019      AUD          19210        1892185.000 98.500
2  03-01-2019      COP           5000         116279.070  0.043
3  04-01-2019      CAD            200          10204.082  0.020
4  05-01-2019      BND           2300           2371.134  0.970

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    相关资源
    最近更新 更多