pandas 熊猫:如何使用 _iLocIndexer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30447719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:23:46  来源:igfitidea点击:

pandas: How to work with _iLocIndexer?

pythonpython-2.7pandas

提问by user650654

>>> import pandas as pd
>>> pd.__version__
'0.16.1'
>>> s1 = pd.Series([1, 3, 5], index=list('abc'))
>>> s1.iloc(0)
<pandas.core.indexing._iLocIndexer object at 0x10ca3f690>

I expected the integer 1 to be returned for s1.iloc(0), but got an _iLocIndexerobject instead. What can I do with this object? How do I work with it?

我希望为 返回整数 1 s1.iloc(0),但得到了一个_iLocIndexer对象。我可以用这个对象做什么?我如何使用它?

回答by user650654

The solution, due to @JohnE, is to use square brackets [0]instead of (0)with iloc.

由于@JohnE,解决方案是使用方括号[0]而不是(0)with iloc

Some more info after digging through some pandas code.

挖掘一些Pandas代码后,还有更多信息。

s1.ilocmakes iloclook like a method. But it is an attribute and the value of the attribute is _iLocIndexer. _iLocIndexeris callable with arguments and it returns a copy of itself ignoring any args or kwargs (see pandas.core.indexing._NDFrameIndexer.callin pandas code). This is why s1.iloc(0)is a valid call.

s1.ilociloc看起来像一种方法。但它是一个属性,该属性的值为_iLocIndexer_iLocIndexer是可调用带参数和返回(见pandas.core.indexing._NDFrameIndexer,其自身的副本忽略任何ARGS或kwargs呼叫在大Pandas代码)。这就是s1.iloc(0)有效调用的原因。

When s1.iloc[0]is called with square brackets, an _iLocIndexeris instantiated and the call to []operator results in a call to the iLocIndexer's `getitem' method.

s1.iloc[0]用方括号调用时, an_iLocIndexer被实例化并且对[]operator 的调用导致对iLocIndexer“getitem”方法的调用。

The same is true for the other indexers - loc, ix, atand iat. These indexers are installed using setattrin class method pandas.core.generic.NDFrame._create_indexer.

这同样适用于其他索引真的- ,,和。这些索引器是使用类方法 pandas.core.generic.NDFrame._create_indexer安装的。locixatiatsetattr