我来自C++背景,最近开始学习python.我正在研究索引和选择数据.我碰到.iloc[]
的类Series
,DataFrame
并Panel
在大熊猫库.我无法理解是什么.iloc
?是功能还是属性?很多次我错误地使用()
而[]
不是得到实际结果(但它不会给我一个错误).
例:
In [43]: s = pd.Series(np.arange(5), index=np.arange(5)[::-1], dtype='int64')
In [44]: s[s.index.isin([2, 4, 6])]
Out[44]:
4 0
2 2
dtype: int64
In [45]: s.iloc(s.index.isin([2,4,6]))
Out[45]: <pandas.core.indexing._iLocIndexer at 0x7f1e68d53978>
In [46]: s.iloc[s.index.isin([2,4,6])]
Out[46]:
4 0
2 2
dtype: int64
Run Code Online (Sandbox Code Playgroud)
谁能告诉我在哪里学习更多关于这类运营商的信息.
实际的答案: 您应该将python列表和字典分别作为iloc
和loc
熊猫扩展,并将其视为查找而不是函数或方法调用。因此,与python语法保持一致,请始终使用[]
而不是()
。
>>> ser = pd.Series( { 'a':3, 'c':9 } )
>>> ser.loc['a'] # pandas dictionary syntax (label-based)
3
>>> ser.iloc[0] # pandas list/array syntax (location-based)
3
Run Code Online (Sandbox Code Playgroud)
对于数据框,基本上是相同的,只是要指定一个额外的维度,这也是在其中iloc
并且loc
变得更加有用的方法,但这超出了此问题的范围。
更深入的回答: 如果您确实想更深入地理解这一点,则需要了解__getitem__
。您也许可以从这里开始一些基本知识。@ayhan在上面的评论中提供的第二个链接中的答案也非常好,并且与您的问题非常相关。
.iloc
是一个类实例。
pd.DataFrame().iloc
Out[2]: <pandas.core.indexing._iLocIndexer at 0x97a2470>
Run Code Online (Sandbox Code Playgroud)
来源:Pandas 源代码 -indexing.py#L1626
归档时间: |
|
查看次数: |
1215 次 |
最近记录: |