Search code examples
pythonpandasdatabaseapplyseries

How solve: "ValueError: The truth value of a Series is ambiguous." with no operators errors?


I'm categorizing a df, and trying to save the category in a column, but i am recienving this error:


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


Here's the code:

import pandas as pd
import numpy as np
df=pd.read_csv(r'C:\Users\gabri\Downloads\credit_scoring_eng.csv')

def assign_status(row):

    if df['income_type']=='unemployed':
        return 'N'
        
    if df['debt']==1:
        return 'N'
    elif df['total_income']>4000 & df['children']==0:
        return 'Y'
    elif df['total_income']>8000 & df['children']==1:
        return 'Y'
    elif df['total_income']>10000 & df['children']==2:
        return 'Y'
    elif df['total_income']>12000 & df['children']==3:
        return 'Y'

df['results']=df.apply(assign_status, axis=1)
print(df.head(10))

Here's the error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [133], in <cell line: 26>()
     23     elif df['total_income']>12000 & df['children']==3:
     24         return 'Aceito'
---> 26 df['results']=df.apply(assign_status)
     27 # Exiba as 10 primeiras linhas
     28 print(df.head(10))

File ~\anaconda3\lib\site-packages\pandas\core\generic.py:1527, in NDFrame.__nonzero__(self)
   1525 @final
   1526 def __nonzero__(self):
-> 1527     raise ValueError(
   1528         f"The truth value of a {type(self).__name__} is ambiguous. "
   1529         "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1530     )

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

I am expecting get a new column with results 'Y' or 'N'for each line.


Solution

  • within your assign_status function, make reference to the row instead of the df Credits: @Naveed :)