Shailesh
Shailesh

Reputation: 2276

Why doesn't iLocation based boolean indexing work?

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?

Upvotes: 4

Views: 4152

Answers (1)

jezrael
jezrael

Reputation: 862671

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]

Upvotes: 6

Related Questions