如果重复这个问题,我深表歉意,我无法找到以前答案的有效解决方案。问题:我想从多索引数据框中删除一列。有问题的列显示为一个级别。特别是我希望删除整个列 'typeE'方法我试图使用 .drop() 删除它,但必须缺少一些规范 (df.drop(['typeE'],axis=1, level=0))这就是 df 的结构。我想删除 typeE 列(全部)[Input] df.columns[Output] MultiIndex(levels=[['typeA', 'typeB', 'typeC', 'typeD', 'typeE', 'typeF'], ['stock', 'bond', 'cash']], labels=[[0, 1, 2, 3, 4, 5, 6], [1, 0, 1, 2, 0, 2]])[Input] df.index[Output] DatetimeIndex(['2005-06-30', '2005-07-01', '2005-07-02'], dtype='datetime64[ns]', length=3, freq=None)[Input] df[Output] typeA typeB typeC typeD typeE typeF bond stock bond cash stock cash2005-06-30 0.000132 0.358719 0.018888 0.132677 0.034894 0.0993452005-07-01 0.000167 0.353419 0.018719 0.139574 0.018923 0.0248922005-07-02 0.002300 0.357893 0.011425 0.130605 0.037289 0.028304
1 回答

慕神8447489
TA贡献1780条经验 获得超1个赞
你可以试试drop
df = df.drop(('typeE','stock'),1)
df
Out[14]:
index typeA typeB typeC typeD typeF
0 bond stock bond cash cash
2005-06-30 0.000132 0.358719 0.018888 0.132677 0.099345
2005-07-01 0.000167 0.353419 0.018719 0.139574 0.024892
2005-07-02 0.002300 0.357893 0.011425 0.130605 0.028304
添加回答
举报
0/150
提交
取消