熊猫基于if / elif / else条件的新列

时间:2020-08-25 06:13:13

标签: python pandas

我想实现类似以下代码的内容。它将基于同一数据框中其他列的值在同一数据框中创建新列notional_amount_jpy

if df2['quoted_currency'] == 'JPY':
    df2['notional_amount_jpy'] = df2['executed_price'] * df2['quantity']
elif df2['quoted_currency'] == 'USD':
    df2['notional_amount_jpy'] = df2['executed_price'] * df2['quantity'] / df_rates['value']
else:
    df2['notional_amount_jpy'] = (df2['executed_price'] * df2['quantity']) * df2['quoted_value'] / df_rates['value']

但是我收到以下错误:

Traceback (most recent call last):
  File "/Users/de/source/GitHub/market_risk/mark_to_mar/src/code/mark_to_mar.py", line 107, in <module>
    if df2['quoted_currency'] == 'JPY':
  File "/Users/de/Library/Python/3.7/lib/python/site-packages/pandas/core/generic.py", line 1556, in __nonzero__
    self.__class__.__name__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

任何帮助将不胜感激。

0 个答案:

没有答案