Pandas:将汇率查找乘以另一个数据框中的相同日期后,按每日金额求和和汇总
Pandas: Sum and aggregate by daily amounts after multiplying exchange rates look-up by same dates from another dataframe
提前感谢您的帮助。
我是 Python 的新手,我尝试了不同的方法来达到要求(主要是使用 groupby()
),但到目前为止一切都失败了。
我有一个数据框,其中包含同一天以不同货币进行的多笔交易(116200 行):
Index
Account No
Withdrawal AMT
Deposit AMT
Dates
Currency
0
12345567
100
300
2015-01-01
eur
1
12345567
100
300
2015-01-01
usd
2
12345567
100
300
2015-01-01
gbp
3
12345567
100
300
2015-01-01
eur
4
34334123
100
300
2015-01-02
usd
5
34334123
100
300
2015-01-02
gbp
我有两个单独的数据框,其中包含每天的汇率(一个是欧元兑英镑,一个是美元兑英镑):
Index
EURO-GBP
Dates
0
1.634
2015-01-01
1
1.6676
2015-01-02
2
1.4554
2015-01-03
3
1.23455
2015-01-04
Index
USD-GBP
Dates
0
0.934
2015-01-01
1
0.943
2015-01-02
2
0.834
2015-01-03
3
0.945
2015-01-04
首先,我需要想办法将数据框的第一个值转换为 GBP。正如您所注意到的,每天都有不同货币的交易,所以任何关于如何做到这一点的提示都将不胜感激!
然后,我想创建一个数据框,同一天每行只有一天,即将每一行与相应的提款和存款列的每日总和合并:
Index
Withdrawal AMT
Deposit AMT
Dates
Currency
0
1000
600
2015-01-01
GBP
1
3000
500
2015-01-02
GBP
2
2000
700
2015-01-03
GBP
再次感谢您花时间阅读我的post!
P.S。所有数字都是随机的!
您可以按照以下方式进行:
(假设您的主数据框名为 df1
,汇率数据框为 df_xr_eur
和 df_xr_usd
):
# Split the main dataframe by currency
df1_eur = df1[df1['Currency'] == 'eur'].copy()
df1_usd = df1[df1['Currency'] == 'usd'].copy()
df1_gbp = df1[df1['Currency'] == 'gbp'].copy()
# Calculate GBP equivalent of currency values
df1_eur['Withdrawal AMT'] *= df1_eur['Dates'].map(df_xr_eur.set_index('Dates')['EURO-GBP'])
df1_eur['Deposit AMT'] *= df1_eur['Dates'].map(df_xr_eur.set_index('Dates')['EURO-GBP'])
df1_usd['Withdrawal AMT'] *= df1_usd['Dates'].map(df_xr_usd.set_index('Dates')['USD-GBP'])
df1_usd['Deposit AMT'] *= df1_usd['Dates'].map(df_xr_usd.set_index('Dates')['USD-GBP'])
# Assemble the previously split datrframes after exchange rate calculation
df2 = pd.concat([df1_eur, df1_usd, df1_gbp]).assign(Currency='GBP')
# Aggregate by `Dates`
df_final = df2.groupby('Dates').agg({'Withdrawal AMT': 'sum',
'Deposit AMT': 'sum',
'Currency': 'first'
}).reset_index()
结果:
print(df_final)
Dates Withdrawal AMT Deposit AMT Currency
0 2015-01-01 520.2 1560.6 GBP
1 2015-01-02 194.3 582.9 GBP
提前感谢您的帮助。
我是 Python 的新手,我尝试了不同的方法来达到要求(主要是使用 groupby()
),但到目前为止一切都失败了。
我有一个数据框,其中包含同一天以不同货币进行的多笔交易(116200 行):
Index | Account No | Withdrawal AMT | Deposit AMT | Dates | Currency |
---|---|---|---|---|---|
0 | 12345567 | 100 | 300 | 2015-01-01 | eur |
1 | 12345567 | 100 | 300 | 2015-01-01 | usd |
2 | 12345567 | 100 | 300 | 2015-01-01 | gbp |
3 | 12345567 | 100 | 300 | 2015-01-01 | eur |
4 | 34334123 | 100 | 300 | 2015-01-02 | usd |
5 | 34334123 | 100 | 300 | 2015-01-02 | gbp |
我有两个单独的数据框,其中包含每天的汇率(一个是欧元兑英镑,一个是美元兑英镑):
Index | EURO-GBP | Dates |
---|---|---|
0 | 1.634 | 2015-01-01 |
1 | 1.6676 | 2015-01-02 |
2 | 1.4554 | 2015-01-03 |
3 | 1.23455 | 2015-01-04 |
Index | USD-GBP | Dates |
---|---|---|
0 | 0.934 | 2015-01-01 |
1 | 0.943 | 2015-01-02 |
2 | 0.834 | 2015-01-03 |
3 | 0.945 | 2015-01-04 |
首先,我需要想办法将数据框的第一个值转换为 GBP。正如您所注意到的,每天都有不同货币的交易,所以任何关于如何做到这一点的提示都将不胜感激!
然后,我想创建一个数据框,同一天每行只有一天,即将每一行与相应的提款和存款列的每日总和合并:
Index | Withdrawal AMT | Deposit AMT | Dates | Currency |
---|---|---|---|---|
0 | 1000 | 600 | 2015-01-01 | GBP |
1 | 3000 | 500 | 2015-01-02 | GBP |
2 | 2000 | 700 | 2015-01-03 | GBP |
再次感谢您花时间阅读我的post!
P.S。所有数字都是随机的!
您可以按照以下方式进行:
(假设您的主数据框名为 df1
,汇率数据框为 df_xr_eur
和 df_xr_usd
):
# Split the main dataframe by currency
df1_eur = df1[df1['Currency'] == 'eur'].copy()
df1_usd = df1[df1['Currency'] == 'usd'].copy()
df1_gbp = df1[df1['Currency'] == 'gbp'].copy()
# Calculate GBP equivalent of currency values
df1_eur['Withdrawal AMT'] *= df1_eur['Dates'].map(df_xr_eur.set_index('Dates')['EURO-GBP'])
df1_eur['Deposit AMT'] *= df1_eur['Dates'].map(df_xr_eur.set_index('Dates')['EURO-GBP'])
df1_usd['Withdrawal AMT'] *= df1_usd['Dates'].map(df_xr_usd.set_index('Dates')['USD-GBP'])
df1_usd['Deposit AMT'] *= df1_usd['Dates'].map(df_xr_usd.set_index('Dates')['USD-GBP'])
# Assemble the previously split datrframes after exchange rate calculation
df2 = pd.concat([df1_eur, df1_usd, df1_gbp]).assign(Currency='GBP')
# Aggregate by `Dates`
df_final = df2.groupby('Dates').agg({'Withdrawal AMT': 'sum',
'Deposit AMT': 'sum',
'Currency': 'first'
}).reset_index()
结果:
print(df_final)
Dates Withdrawal AMT Deposit AMT Currency
0 2015-01-01 520.2 1560.6 GBP
1 2015-01-02 194.3 582.9 GBP