How to fixed 'ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' when & is used

How to fixed 'ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' when & is used

我想根据多列条件重新分配值,但是 ValueError 出现了。我使用 & 而不是 and,这通常是解决此类错误的答案。我的目标和代码如下:

ValueError: The truth value of a DataFrame is ambiguous. 
Use a.empty, a.bool(), a.item(), a.any() or a.all().

如果 CountryBorn == CanadaYearsInCanada == None

,我想重新编码 YearsInCanada == Age
df
Age    CountryBorn    YearsInCanada
87     NaN            77      
67     Canada         67
29     US             7
26     US             10
22     US             12
35     Canada         NaN
45     Canada         NaN

expected output
Age    CountryBorn    YearsInCanada
87     NaN            77      
67     Canada         67
29     US             7
26     US             10
22     US             12
35     Canada         35
45     Canada         45

我的以下代码显示 ValueError

    if df.loc[(df['YearsInCanada'] == None) & (df['CountryBorn'] == 'Canada')]:
        df['YearsInCanada'] == df['Age']

    else:
        df['YearsInCanada'] == df['YearsInCanada']

谢谢

df.loc[(df['YearsInCanada'] == None) & (df['CountryBorn'] == 'Canada')]

这个returns一个(派生的)数据框。 if 需要一个布尔表达式。

如果您尝试进行矢量化分配,则必须将其编码为 Pandas 操作 + 过滤器,而不是 Python if.

使用np.where()的解决方案:

df.YearsInCanada=np.where((df['YearsInCanada'].isna()) & (df['CountryBorn'] == 'Canada'),\
                      df.YearsInCanada.fillna(df.Age),df.YearsInCanada)
print(df)

   Age CountryBorn  YearsInCanada
0   87         NaN           77.0
1   67      Canada           67.0
2   29          US            7.0
3   26          US           10.0
4   22          US           12.0
5   35      Canada           35.0
6   45      Canada           45.0