【问题标题】:deleting a dataframe row by MultiIndex value通过 MultiIndex 值删除数据框行
【发布时间】:2019-07-28 02:19:47
【问题描述】:

从DataFrame 和MultiIndex 的布尔组合,我想删除没有任何索引值为真的行:

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

【问题讨论】:

    标签: pandas indexing multi-index


    【解决方案1】:

    通过MultiIndex.to_frame 将MultiIndex 转换为DataFrame,删除反转~ 并通过axis=1 每行检查any:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 2019-05-15
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多