为什么.loc具有片的包容性行为?
问题说明
由于某些原因,以下两个对iloc
/loc
的调用会产生不同的行为:
For some reason, the following 2 calls to iloc
/ loc
produce different behavior:
>>> import pandas as pd
>>> df = pd.DataFrame(dict(A=range(3), B=range(3)))
>>> df.iloc[:1]
A B
0 0 0
>>> df.loc[:1]
A B
0 0 0
1 1 1
我知道loc
考虑行标签,而iloc
考虑行的基于整数的索引.但是为什么loc
调用的上限被认为是包含的,而iloc
调用的上限却被认为是排他的呢?
I understand that loc
considers the row labels, while iloc
considers the integer-based indices of the rows. But why is the upper bound for the loc
call considered inclusive, while the iloc
bound is considered exclusive?
正确答案
快速解答:
使用标签时,进行端到端切片通常更有意义,因为它需要有关DataFrame中其他行的知识较少.
It often makes more sense to do end-inclusive slicing when using labels, because it requires less knowledge about other rows in the DataFrame.
每当您关心标签而不是位置时,末端排他的标签切片都会以一种不方便的方式引入位置依赖性.
Whenever you care about labels instead of positions, end-exclusive label slicing introduces position-dependence in a way that can be inconvenient.
更长的答案:
任何函数的行为都是一个权衡:您偏爱某些用例而不是其他用例.最终,.iloc
的操作是熊猫开发人员的主观设计决定(正如@ALlollz的评论所指出的,此行为 @Willz的答案:
That said, maybe there are use cases where you really do want end-exclusive label-based slicing. If so, you can use @Willz's answer in this question:
df.loc[start:end].iloc[:-1]
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfgihc
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22