Python 索引。未实现非唯一索引上的连接级别

Python 索引。未实现非唯一索引上的连接级别,python,pandas,dataframe,hierarchical-data,Python,Pandas,Dataframe,Hierarchical Data,这是我的数据 我的数据帧名称是tab user_id len barcode 0 1 490 1 2 71 2 3 1 3 5 1 这是我的密码 tab = tab[tab[('len', 'barcode')]] tab.columns MultiIndex(levels=[['len', 'user_id'], ['bar

这是我的数据

我的数据帧名称是
tab

        user_id     len
        barcode
0        1          490
1        2          71
2        3          1
3        5          1
这是我的密码

tab = tab[tab[('len', 'barcode')]]
tab.columns

MultiIndex(levels=[['len', 'user_id'], ['barcode', '']],
           labels=[[1, 0], [1, 0]])

这段代码在我的日常笔记本上运行了10多次,但它不再运行了

我认为有一个问题,您的第二级名称是空字符串

因此,我们需要:

a = tab[('len', '')]
print (a)
0    490
1     71
2      1
3      1
Name: (len, ), dtype: int64
如果要替换列名称中的所有空字符串:

tab = tab.rename(columns={'':'b'})
a = tab[('len', 'b')]
print (a)
0    490
1     71
2      1
3      1
Name: (len, b), dtype: int64
但对于删除列中的多索引,最好是删除
[]

tab = pd.pivot_table(barcode,index="user_id",values="barcode",‌​aggfunc='size')
tab = tab.reset_index()

什么是
df.columns
?问题编辑它来自数据透视
tab=pd.pivot\u表(条形码,索引=[“用户id”],值=[“条形码”],aggfunc=[len])
tab=tab.reset\u index()
是的,现在我明白了。检查我的答案的最后一次编辑。好的,很好的解释,这很有帮助