我注意到在Pandas中对DataFrame进行子集化时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
为什么在使用NaN返回行时df.loc[[7]]
抛出错误df.ix[[7]]
?这是一个错误吗?如果不是,为什么loc
而ix
这样设计?
(注意我在Python 3.5.1上使用Pandas 0.17.1)