Ame*_*ina 20 python numpy pandas
lexsort_depth多索引数据帧究竟是什么?为什么必须对索引进行排序?
例如,我注意到,在手动构建包含df三个级别的列的多索引数据框之后,如果我尝试执行此操作:
idx = pd.IndexSlice
df[idx['foo', 'bar']]
Run Code Online (Sandbox Code Playgroud)
我明白了:
KeyError: 'Key length (2) was greater than MultiIndex lexsort depth (0)'
Run Code Online (Sandbox Code Playgroud)
而在这一点上,df.columns.lexsort_depth是0
df = df.sortlevel(0,axis=1)
Run Code Online (Sandbox Code Playgroud)
然后横截面索引工作.为什么?究竟是什么lexsort_depth,为什么排序sortlevel修复这种类型的索引?