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

Time:10-13

I have a problem when I try to add a column to a dataframe, but I get an error. What's wrong with my function? Note: PAVARDE in lithuanian means surname. Dataframe is about President elections: https://www.vrk.lt/en/2019-prezidento/rezultatai (Election results in the second poll (csv)). Function:

def status(person):
    PAVARDE = person
    if PAVARDE == 'NAUSĖDA':
        return 'President'
    else:
        return 'looser'

president_df['winner'] = president_df[['PAVARDE']].apply(status,axis=1)

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

What's wrong? Could you help me?

CodePudding user response:

You are passing the whole column to the function but you need to pass value by value. At first you need to remove extra square brackets, an then when you working with pd.Series you don't need axis argument anymore. In the end your code should look like this:

def status(person):
    PAVARDE = person
    if PAVARDE == 'NAUSĖDA':
        return 'President'
    else:
        return 'looser'

president_df['winner'] = president_df['PAVARDE'].apply(status)

or like this:

df['winner'] = df['PAVARDE'].apply(lambda x: 'President' if x == 'NAUSĖDA' else 'looser')
  • Related