这是我的多索引数据框:
A B
Net Upper Lower Mid Zsore
Answer option
More than once a day 0% 0.22% -0.12% 2 65
Once a day 0% 0.32% -0.19% 3 45
Several times a week 2% 2.45% 1.10% 4 78
Once a week 1% 1.63% -0.40% 6 65
我想在第二级建立一个COLUMN NAME的子集(没什么花哨的),只保留以下列“Net,Upper,Zscore”
Net Upper Zsore
Answer option
More than once a day 0% 0.22% 65
Once a day 0% 0.32% 45
Several times a week 2% 2.45% 78
Once a week 1% 1.63% 65
我尝试将其关闭失败,我猜它失败了,因为我不知道如何解释2个级别的列?
df = df[[u'Net',u'Upper',u'Zsore']]
答案 0 :(得分:2)
也许:
>>> j = df.columns.get_level_values(1).isin(['Net', 'Upper', 'Zsore'])
>>> df.loc[:,j]
A B
Net Upper Zsore
Answer option
More than once a day 0% 0.22% 65
Once a day 0% 0.32% 45
Several times a week 2% 2.45% 78
Once a week 1% 1.63% 65