Python 使用pandas对多个映射列执行按列操作
我有两个数据帧,即df1和df2。我想对df2中的“新金额”列执行操作。基本上,在df1中,我有历史货币数据,我希望在df2中给定货币和金额时执行日期操作,以计算df2中新的金额列的值 对于“货币”=[AUD,BWP]我们需要将金额乘以相应日期的相应货币值 对于其他货币,我们需要将金额除以相应日期的相应货币值 e、 g在df2中,日期为'01-01-2019'的第一种货币为澳元,因此我想计算新的金额和美元价值,以便 新金额=金额*来自df1的AUD值,即新金额=19298*98=1891204 另一个例子是,在df2中,日期='03-01-2019,我有第三种货币作为COP,所以我想计算新的金额和美元价值,以便 新金额=df1的金额/COP值,即新金额=5000/0.043=116279.06Python 使用pandas对多个映射列执行按列操作,python,pandas,Python,Pandas,我有两个数据帧,即df1和df2。我想对df2中的“新金额”列执行操作。基本上,在df1中,我有历史货币数据,我希望在df2中给定货币和金额时执行日期操作,以计算df2中新的金额列的值 对于“货币”=[AUD,BWP]我们需要将金额乘以相应日期的相应货币值 对于其他货币,我们需要将金额除以相应日期的相应货币值 e、 g在df2中,日期为'01-01-2019'的第一种货币为澳元,因此我想计算新的金额和美元价值,以便 新金额=金额*来自df1的AUD值,即新金额=19298*98=1891204
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
df2
预期结果
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
您需要
查找
和isin()
:
输出:
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
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
您需要
查找
和isin()
:
输出:
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
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
尝试使用
melt
和merge
:
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
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
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
尝试使用
melt
和merge
:
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
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
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
如何判断何时将df1中的值与d2相乘或相除?i、 e.如何区分与AUD相乘与与与CAD相除?如果它小于1除以?不,它可以是任何不必小于1的值。如果货币为AUD或BWP,则乘以货币值,否则除以如何判断何时将df1中的值乘以或除以d2?i、 e.如何区分与AUD相乘与与与CAD相除?如果它小于1除以?不,它可以是任何不必小于1的值。如果货币为澳元或BWP,则乘以货币值,否则除以如果df1中缺少任何货币,我想在“新金额”中使用相同的“金额”。如果df1中缺少任何货币,我想使用相同的“金额”,该怎么办在“新金额”中。