在单独的 pandas 数据框中乘以列

Multiplying columns in separate pandas dataframes

我正在尝试将来自 2 个不同数据帧的数据和我的代码相乘,如下所示:

import pandas as pd
import numpy as np

df1 = pd.DataFrame({'v_contract_number': ['VN120001438','VN120001439',
                                          'VN120001440','VN120001438',
                                          'VN120001439','VN120001440'],
                                            'Currency': ['VND','USD','KRW','USD','KRW','USD'],
                                        'Amount': [10000,5000,6000,200,150,175]})
df2 = pd.DataFrame({'Currency': ['VND','USD','KRW'],'Rate': [1,23000,1200]})
print(df1)

# df1
  v_contract_number Currency  Amount
0       VN120001438      VND   10000
1       VN120001439      USD    5000
2       VN120001440      KRW    6000
3       VN120001438      USD     200
4       VN120001439      KRW     150
5       VN120001440      USD     175

print(df2)
  Currency   Rate
0      VND      1
1      USD  23000
2      KRW   1200

df1 = df1.merge(df2)
df1['VND AMount'] = df1['Amount'].mul(df1['Rate'])
df1.drop('Rate', axis=1, inplace=True)
print(df1)

# result
  v_contract_number Currency  Amount  VND AMount
0       VN120001438      VND   10000       10000
1       VN120001439      USD    5000   115000000
2       VN120001438      USD     200     4600000
3       VN120001440      USD     175     4025000
4       VN120001440      KRW    6000     7200000
5       VN120001439      KRW     150      180000

这正是我想要的,但我想知道还有另一种方法可以不像我那样合并和删除吗? 我删除“评分”的原因是因为我不希望它出现在我的报告中。

谢谢并致以最诚挚的问候

您可以为此使用 pandas' 地图:

df2 = df2.set_index('Currency').squeeze() # squeeze converts to a Series

df1.assign(VND_Amount = df1.Amount.mul(df1.Currency.map(df2)))

  v_contract_number Currency  Amount  VND_Amount
0       VN120001438      VND   10000       10000
1       VN120001439      USD    5000   115000000
2       VN120001440      KRW    6000     7200000
3       VN120001438      USD     200     4600000
4       VN120001439      KRW     150      180000
5       VN120001440      USD     175     4025000

您可以通过在合并操作中不覆盖 df1 来避免删除:

df1["VND Amount"] = df1.merge(df2, on="Currency").eval("Amount * Rate")

或者,您可以使用 .reindex 根据货币列将 df2 与 df1 对齐:

df1["VND Amount"] = (
    df1["Amount"] * 
    (df2.set_index("Currency")["Rate"]  # set the index and return Rate column
        .reindex(df1["Currency"])       # align "Rate" values to df1 "Currency"
        .to_numpy()                     # get numpy array to avoid pandas 
                                        #   auto alignment on math ops
    )
)