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

Question:

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?

Answers:

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')
Answered By: Masha

@Masha,

Thank you so much for your reply and help, it works!

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.