Home > OS >  How solve: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.
How solve: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.

Time:11-16

I am making a function to categorize data, but i am recieving this error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

import pandas as pd

df=pd.read_csv(r'C:\Users\gabri\Downloads\credit_scoring_eng.csv')


def economic_class(valor):
    if valor<=16000:
        return 'economic_class'
    elif valor<=24000:
        return 'executive_class'
    elif valor<=32000:
        return 'first_class'
    else:
        return 'five_star_class'
df['class'] = df['total_income'].apply(economic_class)


Here is the complete error:

Cell In [172], line 3
      1 # Criar coluna com categorias
      2 print(df['total_income'])
----> 3 df['class'] = df['total_income'].apply(economic_class)

File c:\Users\gabri\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py:4433, in Series.apply(self, func, convert_dtype, args, **kwargs)
   4323 def apply(
   4324     self,
   4325     func: AggFuncType,
   (...)
   4328     **kwargs,
   4329 ) -> DataFrame | Series:
   4330     """
   4331     Invoke function on values of Series.
   4332 
   (...)
   4431     dtype: float64
   4432     """
-> 4433     return SeriesApply(self, func, convert_dtype, args, kwargs).apply()

File c:\Users\gabri\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\apply.py:1082, in SeriesApply.apply(self)
   1078 if isinstance(self.f, str):
   1079     # if we are a string, try to dispatch
...
   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 already try a lot of solutions of this error on internet, but didnt work ;-;

I was expecting make a new column to the dataframe.

df.head()

CodePudding user response:

Try the below. It's also helpful to provide either dummy data or a sample of your data(not an image).

Without data provided, below is a sample you can adjust that works

import pandas as pd

data = {'Name':['Bob','Kyle','Kevin','Dave'],
        'Value':[1000,20000,40000,30000]}

df = pd.DataFrame(data)
display(df)


def economic_class(valor):
    if valor<=16000:
        valor = 'economic_class'
        
    elif valor<=24000:
        valor = 'executive_class'
        
    elif valor<=32000:
        valor = 'first_class'
        
    else:
        valor = 'five_star_class'
        
    return valor

df['class'] = df['Value'].apply(economic_class)
display(df)

CodePudding user response:

Your function is correct. I think the problem is apply. Can you try this:

def economic_class(valor):
    if valor<=16000:
        return 'economic_class'
    elif valor<=24000:
        return 'executive_class'
    elif valor<=32000:
        return 'first_class'
    else:
        return 'five_star_class'
df['class'] = df['total_income'].apply(lambda x: economic_class(x))
  • Related