为什么.ix包含在索引范围的末尾?

时间:2017-06-23 14:40:51

标签: python pandas numpy

Python版本:2.7.6 Numpy版本:1.10.2 熊猫:0.17.1

我知道.ix现已弃用,但我正在使用遗留系统并看到这种行为与.ix和我预先准备好了

# Native Python List Indexing is exclusive on the end index
[0, 1, 2, 3][0:1] # returns [0] indexes with [0, 1)
# Native Numpy
import numpy as np
numpyArray = np.reshape(np.arange(4), (2, 2))
numpyArray[0:1, 0:1] # returns array([[0]]), indexes with [0, 1) in rows and [0, 1) in columns
####### Pandas #######
import pandas as pd
dataFrame = pd.DataFrame(numpyArray) 
# Pandas with iloc #
dataFrame.iloc[0:1, 0:1] # returns 0, indexes with [0, 1) in rows and [0, 1) in columns
# Pandas with ix #
dataFrame.ix[0:1, 0:1] # returns [[0, 1], [2, 3] indexes with [0, 1] in rows and [0, 1] in columns

1 个答案:

答案 0 :(得分:4)

.ix是基于标签的索引(与.loc相同),文档状态包括与iloc不同的停止范围值,这是开放闭合范围所以不是包括停止范围值,这是设计

这样做的原因是因为如果您的索引是例如字符串,那么选择一个您不知道结束范围值应该是什么的范围会有问题:

In[274]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'), index=list('vwxyz'))
df

Out[274]: 
          a         b         c
v -0.488627  0.213183  0.224104
w -0.200328 -1.138937  0.815568
x -1.131868 -0.562758  0.088719
y  0.120701 -0.863737  0.246295
z -0.808140  0.253376  0.645974

In[275]:
df.ix['w':'y']

Out[275]: 
          a         b         c
w -0.200328 -1.138937  0.815568
x -1.131868 -0.562758  0.088719
y  0.120701 -0.863737  0.246295

如果它没有包含最后一行的结束值,您需要知道必须传递'z'才能在'z'之前返回标签以获得上述结果

<强>更新

请注意0.20.1已弃用loc,您应使用var user = new Parse.User(); var base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE="; var imageFile = new Parse.File("image.png", { base64: base64 }); user.set("username", $scope.user.username); user.set("password", $scope.user.password); user.set("email", $scope.user.email); user.set("picture", imageFile);//getting file need url user.signUp(null, { success: function (user) { $state.go('app.home'); }, error: function (user, error) { alert("Error: " + error.code + " " + error.message); } });