0
votes

I have a columns of data in a pandas dataframe 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.

The data looks like the following:

Excel image

1
Please provide the additional information needed for someone to recreate this error.Scott Hunter
Welcome to Stack Overflow. What is data? Are you using a nonstandard library like numpy or pandas? Please read How to Ask.Chris
Please provide an example of the data you are working with. From your description, it is unclear what the data looks like.squareskittles

1 Answers

0
votes

So you are looking at a DataFrame with one column or a Series? In this case, try using .values, like so:

def ad_val_rate(data):
    if data['2021'].values == 'Free':
        […]

If instead there's more than one column in your dataframe, you need to iterate.