Why doesn't iLocation based boolean indexing work?

时光怂恿深爱的人放手 提交于 2021-02-11 08:40:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!