IF 语句 Pandas Dataframe:Series 的真值不明确

IF statement Panda Dataframe: The truth value of a Series is ambiguous

我有一个只有浮点数据的数据框。我基本上想创建一个新列,如果它满足条件,它将从另一列获取值,如果不满足,则从另一列获取值。 我所有的专栏都是浮点型的。

for col in list_scenarios:
    df_merged['in_scenario_'+ col] = 1.0
    print(df_merged['in_scenario_' + col])
    if df_merged[col].shift(1)== 1.0:
        df_merged['in_scenario_' + col] = df_merged['in_scenario_'+ col].shift(1)*df_merged[asset +'_r']
    else:
        df_merged['in_scenario_' + col] = df_merged['in_scenario_'+ col].shift(1)
    print(df_merged['in_scenario_' + col])

我收到以下错误:

    Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pydev\pydev_run_in_console.py", line 52, in run_file
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Main/PycharmProjects/Macrobond_API/scenario testing.py", line 268, in <module>
    if df_merged[col].shift(1)== 1.0:
  File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\generic.py", line 1121, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我想不通有什么歧义。

谢谢

My dataframe looks like this (month year are indices)
            sp500_500  USA  MEXICO  sp500_500_r
month year                                                           
6     2017   2423.41  1.0      1.0     0.004814
7     2017   2470.30  1.0      1.0     0.019349
8     2017   2471.65  1.0      1.0     0.000546

df_merged[col].shift(1)== 1.0你比较两种不同的类型 df_merged[col].shift(1) return 一个数据框。如果你想获得该列的第一个值,你可以使用 iloc。 df_merged[col].iloc[0] == 1.0

a
   x  
0   0   
1   1   
2   2   
3   3   
4   4   

a.x.shift(1)

   x  
0   NaN   
1   1   
2   2   
3   3   
4   4  

a.x.iloc[0]

1

我实际上找到了解决方法。

for col in list_scenarios:
    df_merged['in_scenario_'+ col] = df_merged[asset +'_r']
    df_merged[col] = df_merged[col].shift(1)
    df_merged.loc[df_merged[col] ==0, 'in_scenario_'+ col] = 0

这给了我想要的'returns'。然后我只需要从这个 returns 以 1 作为第一个值开始构建索引。

感谢您的帮助。