Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't iLocation based boolean indexing work?

Tags:

python

pandas

I was trying to filter a Dataframe and thought that if a loc takes a boolean list as an input to filter, it should also work in the case for iloc. Eg.

import pandas as pd
df = pd.read_csv('https://query.data.world/s/jldxidygjltewualzthzkaxtdrkdvq')

df.iloc[[True,False,True]] #works
df.loc[[True,False,True]] #works

df.loc[df['PointsPerGame'] > 10.0] #works
df.iloc[df['PointsPerGame'] > 10.0] # DOES NOT WORK

The documentation states that both loc and iloc accept a boolean array as an argument.

For iloc iloc

For loc loc

So, I believe this does not work purely because it was not implemented, or is this because of some other reason which I'm failing to understand?

like image 315
Yankee Avatar asked Oct 14 '25 18:10

Yankee


1 Answers

It is not bug:

this is by-definition not allowed. .iloc is purely positional, so it doesn't make sense to align with a passed Series (which is what all indexing operations do).

You need to convert mask to numpy array for iloc, for loc it working too, but not necessary:

df.iloc[(df['PointsPerGame'] > 10.0).values]
like image 149
jezrael Avatar answered Oct 17 '25 06:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!