问题
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
For 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?
回答1:
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]
来源:https://stackoverflow.com/questions/59016143/why-doesnt-ilocation-based-boolean-indexing-work