Search code examples
pythonpandasvalueerror

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


I want to reassign values based on multiple columns conditions, but the ValueError appears. I used & instead of and, which was often the answer to solve such error. My goal and codes are as following:

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

I want to recode YearsInCanada == Age if CountryBorn == Canada and YearsInCanada == None

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

My following codes showed the ValueError

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

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

Thanks


Solution

  • Solution using 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