Cle*_*leb 9 python slice pandas
我对以下行为感到困惑.当我有这样的数据帧时:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6, 4), columns=list('ABCD'), index=list('bcdefg'))
Run Code Online (Sandbox Code Playgroud)
看起来如下:
A B C D
b -0.907325 0.211740 0.150066 -0.240011
c -0.307543 0.691359 -0.179995 -0.334836
d 1.280978 0.469956 -0.912541 0.487357
e 1.447153 -0.087224 -0.176256 1.319822
f 0.660994 -0.289151 0.956900 -1.063623
g -1.880520 1.099098 -0.759683 -0.657774
Run Code Online (Sandbox Code Playgroud)
我收到了预期的错误
TypeError:无法对这些类型为'int'的索引器[3]进行切片索引
当我尝试使用以下切片时.loc
:
print df.loc[3:, ['C', 'D']]
Run Code Online (Sandbox Code Playgroud)
因为我传递一个整数作为索引,而不是包含在其中的一个字母index
.
但是,如果我现在尝试
df.loc[3:, ['C', 'D']] = 10
Run Code Online (Sandbox Code Playgroud)
它工作正常,并给我输出:
A B C D
b -0.907325 0.211740 0.150066 -0.240011
c -0.307543 0.691359 -0.179995 -0.334836
d 1.280978 0.469956 -0.912541 0.487357
e 1.447153 -0.087224 10.000000 10.000000
f 0.660994 -0.289151 10.000000 10.000000
g -1.880520 1.099098 10.000000 10.000000
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么在打印某些内容时相同的命令失败以及为什么它在分配值时起作用.当我检查doc字符串时.loc
,我原本预计这会导致上面提到的错误(尤其是粗体部分):
允许的输入是:
- 单个标签,例如
5
或'a'
(注意,它5
被解释为索引的标签,**从不作为索引的整数位置**).- 标签列表或数组,例如
['a', 'b', 'c']
.- 带有标签的切片对象,例如
'a':'f'
(注意,与通常的python切片相反,包括开始和停止!).- 布尔数组.
- 甲
callable
使用一个参数(主叫系列,数据帧或面板),并且返回用于索引有效输出(上述之一)函数
.loc
KeyError
当找不到物品时会提出.
任何解释; 我在这里想念的是什么?
编辑
在这个问题中,类似的行为被认为是在0.13中修复的错误.我用0.19.1.
编辑2 在@ EdChum的帖子上建立,可以做以下事情:
df.loc[2] = 20
df.loc[3] = 30
df.loc[4] = 40
Run Code Online (Sandbox Code Playgroud)
产量
A B C D
b 0.083326 -1.047032 0.830499 -0.729662
c 0.942744 -0.535013 0.809251 1.132983
d -0.074918 1.123331 -2.205294 -0.497468
e 0.213349 0.694366 -0.816550 0.496324
f 0.021347 0.917340 -0.595254 -0.392177
g -1.149890 0.965645 0.172672 -0.043652
2 20.000000 20.000000 20.000000 20.000000
3 30.000000 30.000000 30.000000 30.000000
4 40.000000 40.000000 40.000000 40.000000
Run Code Online (Sandbox Code Playgroud)
然而,这对我来说仍然令人困惑,因为
print df.loc['d':'f', ['C', 'D']]
Run Code Online (Sandbox Code Playgroud)
工作正常,命令
print df.loc[2:4, ['C', 'D']]
Run Code Online (Sandbox Code Playgroud)
给出上面提到的索引错误.
此外,当一个人现在分配这样的值
df.loc[2:4, ['C', 'D']] = 100
Run Code Online (Sandbox Code Playgroud)
数据框如下所示:
A B C D
b 0.083326 -1.047032 0.830499 -0.729662
c 0.942744 -0.535013 0.809251 1.132983
d -0.074918 1.123331 100.000000 100.000000
e 0.213349 0.694366 100.000000 100.000000
f 0.021347 0.917340 -0.595254 -0.392177
g -1.149890 0.965645 0.172672 -0.043652
2 20.000000 20.000000 20.000000 20.000000
3 30.000000 30.000000 30.000000 30.000000
4 40.000000 40.000000 40.000000 40.000000
Run Code Online (Sandbox Code Playgroud)
所以这些值不会添加到一个 - 或者至少我 - 期望它们被添加(使用位置而不是标签).
我不认为这是一个错误,而是未记录的语义,例如,对于行标签不存在的简单情况,允许进行放大设置:
In [22]:
df.loc[3] = 10
df
Out[22]:
A B C D
b -0.907325 0.211740 0.150066 -0.240011
c -0.307543 0.691359 -0.179995 -0.334836
d 1.280978 0.469956 -0.912541 0.487357
e 1.447153 -0.087224 -0.176256 1.319822
f 0.660994 -0.289151 0.956900 -1.063623
g -1.880520 1.099098 -0.759683 -0.657774
3 10.000000 10.000000 10.000000 10.000000
Run Code Online (Sandbox Code Playgroud)
如果我们传递一个切片,则在切片中找不到标签,但由于它是整数切片,因此它会转换为序数切片:
In [24]:
df.loc[3:5] = 9
df
Out[24]:
A B C D
b -0.907325 0.211740 0.150066 -0.240011
c -0.307543 0.691359 -0.179995 -0.334836
d 1.280978 0.469956 -0.912541 0.487357
e 9.000000 9.000000 9.000000 9.000000
f 9.000000 9.000000 9.000000 9.000000
g -1.880520 1.099098 -0.759683 -0.657774
3 10.000000 10.000000 10.000000 10.000000
Run Code Online (Sandbox Code Playgroud)
您链接的帖子和错误指的是没有分配的选择,其中传递了不存在的标签,这应该引发 a KeyError
,这在这里是不同的
如果我们看一下__setitem__
:
def __setitem__(self, key, value):
key = com._apply_if_callable(key, self)
# see if we can slice the rows
indexer = convert_to_index_sliceable(self, key))
Run Code Online (Sandbox Code Playgroud)
这里它将尝试转换切片调用convert_to_index_sliceable
:
def convert_to_index_sliceable(obj, key):
"""if we are index sliceable, then return my slicer, otherwise return None
"""
idx = obj.index
if isinstance(key, slice):
return idx._convert_slice_indexer(key, kind='getitem')
Run Code Online (Sandbox Code Playgroud)
如果我们查看文档字符串:
签名: df.index._convert_slice_indexer(key, kind=None) 文档字符串:转换切片索引器。禁止在开始/停止/步骤中出现浮动
参数 ---------- key : 切片绑定类型的标签: {'ix', 'loc', 'getitem', 'iloc'} 或 None
然后运行这个:
In [29]:
df.index._convert_slice_indexer(slice(3,5),'loc')
Out[29]:
slice(3, 5, None)
Run Code Online (Sandbox Code Playgroud)
然后使用它来对索引进行切片:
In [28]:
df.index[df.index._convert_slice_indexer(slice(3,5),'loc')]
Out[28]:
Index(['e', 'f'], dtype='object')
Run Code Online (Sandbox Code Playgroud)
因此我们看到,即使您传递了看似不存在的标签,整数切片对象也会根据不同的规则转换为与 df 兼容的序数切片