我注意到在Pandas中对DataFrame进行子集化时,loc
和ix
之间存在奇怪的差异。loc和ix之间的意外差异
import pandas as pd
# Create a dataframe
df = pd.DataFrame({'id':[10,9,5,6,8], 'x1':[10.0,12.3,13.4,11.9,7.6], 'x2':['a','a','b','c','c']})
df.set_index('id', inplace=True)
df
x1 x2
id
10 10.0 a
9 12.3 a
5 13.4 b
6 11.9 c
8 7.6 c
df.loc[[10, 9, 7]] # 7 does not exist in the index so a NaN row is returned
df.loc[[7]] # KeyError: 'None of [[7]] are in the [index]'
df.ix[[7]] # 7 does not exist in the index so a NaN row is returned
为什么df.loc[[7]]
抛出一个错误,而df.ix[[7]]
返回一行与南?这是一个错误?如果没有,为什么loc
和ix
这样设计?
(注意:我使用的是熊猫0.17.1上的Python 3.5.1)
决定这是最有可能的错误。提交报告[这里](https://github.com/pydata/pandas/issues/11840) – Ben