0
我試圖使用.loc訪問基於CategoricalIndex的熊貓數據框的行,但我得到TypeError
。最小非工作的例子是.loc for Pategors中的CategoricalIndex
import pandas as pd
df = pd.DataFrame({'foo': rand(3), 'future_index': [22, 13, 87]})
df['future_index'] = df['future_index'].astype('category')
df = df.set_index('future_index')
然後,在試圖訪問相應的行標記13
df.loc[13]
我得到
TypeError: cannot do label indexing on <class 'pandas.core.indexes.category.CategoricalIndex'> with these indexers [13] of <class 'int'>
儘管
13 in df.index
是True
。我知道我可以最終獲得13的索引
df.index.get_loc(13)
但是,爲什麼上述更簡單的方法不工作?我錯過了什麼?
乾杯。
這看起來喜歡的事,目前如果沒有基於整數CategoricalIndices實現,你的類別是基於字符/ str的,例如「'a','b','c'',然後'df.loc ['a']'將起作用。看起來這是目前的限制 – EdChum