我注意到在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