renakre renakre - 3 months ago 39
Python Question

Selecting rows if the value is in a range: "The truth value of a Series is ambiguous"

I am trying to select a subset of the dataframe by choosing rows only if the Rank value is between 1 and 16 (including 1 and 16). I can achieve this with the following code:

ScimEn = ScimEn.loc[ScimEn["Rank"] < 16]

However, I prefer to use the range function to achieve this:

rankings = range(1,16)
ScimEn.loc[ScimEn["Rank"] in rankings]

However, with this code, I receive this error:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Can someone help me with figuring out what is wrong with the second approach?


Leo Leo

You need to use 17 instead of 16 to get your desired condition.

rankings = range(1,17)