vendredi 29 novembre 2019

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

I have a columns of data that have percentages, such as 5.76%, 4.2%, or 2.34%, and the the word Free. I want to create another column for each of those columns that has a 0 for free and converts the percentages into an integer/fraction, such as 0.0576, 0.042, and 0.0234.

I have created the following function:

def ad_val_rate(data):
if data['2021'] == 'Free':
    return 0
else:
    return data['2021'].str.rstrip('%').astype('float') / 100.0

When I use this function, I get the following error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). Other similar stackoverflow questions seem to indicate the issue is because of more than one condition, but I am just using one condition.

Aucun commentaire:

Enregistrer un commentaire