简体   繁体   English

何时将iloc和loc用于布尔值

[英]when to use iloc and loc for boolean

I'm a bit confusing when using boolean series for indexing for pandas Dataframe. 使用布尔系列为pandas Dataframe编制索引时,我有些困惑。 Should I use iloc or loc? 我应该使用iloc还是loc? or any better solution? 或任何更好的解决方案? for example 例如

t1 = pd.DataFrame(np.ones([3,4]))
t1.iloc[1:3,0]=3

this line will give correct answer 这行会给出正确的答案

t1.loc[:,(t1>2).any()]

but line with iloc wiill raise an error 但是与iloc wiill一致会引发错误

t1.iloc[:,(t1>2).any()]

I check https://pandas.pydata.org/pandas-docs/stable/indexing.html , the page says both iloc and loc accept a boolean array. 我检查https://pandas.pydata.org/pandas-docs/stable/indexing.html ,页面上说iloc和loc都接受布尔数组。 Why does iloc not work in my example? 为什么在我的示例中iloc不起作用? When to use iloc and loc? 何时使用iloc和loc? or are there any better alternatives? 还是有更好的选择?

The nuance is that iloc requires a Boolean array , while loc works with either a Boolean series or a Boolean array. 细微的差别是iloc需要一个布尔数组 ,而loc可以使用布尔序列或布尔数组。 The documentation is technically correct in stating that a Boolean array works in either case. 该文档在技术上是正确的,说明布尔数组在两种情况下均有效。

So, for iloc , extracting the NumPy Boolean array via pd.Series.values will work: 因此,对于iloc ,通过pd.Series.values提取NumPy布尔数组将起作用:

t1.iloc[:, (t1>2).any().values]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM