Error: The truth value of a Series is ambiguous

Viewed 308

I'm trying to generate a new columns using following code

list = ['LHR','-1','-3','LGW','MAD','SIN','KUL','JFK','HKG','PVG','IST','SDA','GLA']
for i in list:
    if plotdata.loc[plotdata['LOCATION'] == i] :
        plotdata['city'] = plotdata['LOCATION']
    else:
        plotdata['city'] = 'others'

I get the following error:

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

The datatype is category, why do I get this error, please?

2 Answers

Not sure why you're getting this error. But, it's best-practice to not loop through pandas.

You could avoid that error by setting the "city" to whatever the "location" is and then reverting back to "others" if it's not in the list.

city_list = ['LHR','-1','-3','LGW','MAD','SIN','KUL','JFK','HKG','PVG','IST','SDA','GLA']
plotdata['city'] = plotdata['LOCATION']
plotdata.loc[~plotdata['city'].isin(city_list), 'city'] = 'other'

You get this error because the following code:

if plotdata.loc[plotdata['LOCATION'] == i]

is testing on a subset of the dataframe plotdata returned by .loc(), which in turns is because the Boolean mask:

plotdata['LOCATION'] == i

returns a Boolean array.

Overall, since .loc() returns a subset of dataframe, hence the message:

ValueError: The truth value of a DataFrame is ambiguous.

Related