从具有DataFrame
的布尔组合的MultiIndex
中,我要删除所有索引值都不为True的行:
T000001 T000025
True False 1430.0
False True 301.0
False False 7950.0
将索引项作为布尔数组处理不起作用:
df[~df.index.any()]
TypeError Traceback (most recent call last)
<ipython-input-35-caeaa0a17799> in <module>
----> 1 combi.index.any()
~/anaconda3/lib/python3.6/site-packages/pandas/core/ops.py in invalid_op(self, other)
212 def invalid_op(self, other=None):
213 raise TypeError("cannot perform {name} with this index type: "
--> 214 "{typ}".format(name=name, typ=type(self).__name__))
215
216 invalid_op.__name__ = name
TypeError: cannot perform any with this index type: MultiIndex
答案 0 :(得分:0)
通过MultiIndex.to_frame
将MultiIndex
转换为DataFrame
,删除反向~
并通过any
每行检查axis=1
:
df = df[df.index.to_frame().any(axis=1)]
print (df)
T000001 T000025
True False 1430.0
False True 301.0
Name: a, dtype: float64