简体   繁体   English

在 Python 的 Pandas 中,如何在 MultiIndex 中的字段上过滤 DataFrame

[英]In Python's Pandas, how do I filter a DataFrame on a Field in a MultiIndex

I have this DataFrame that has a MultiIndex with the fiels Hi , Md , Lo我有这个 DataFrame 有一个 MultiIndex 字段为HiMdLo

import pandas as pd

df = pd.DataFrame(
    [
        ["A", "X", "C", "7", "3"],
        ["A", "B", "C", "7", "2"],
        ["A", "B", "C", "7", "1"],
        ["A", "X", "C", "7", "3"],
        ["1", "2", "3", "7", "3"],
        ["4", "5", "6", "7", "3"],
    ],
    columns=["Hi", "Md", "Lo", "O", "I"],
).set_index(["Hi", "Md", "Lo"])

It looks like this看起来像这样

          O  I
Hi Md Lo      
A  X  C   7  3
   B  C   7  2
      C   7  1
   X  C   7  3
1  2  3   7  3
4  5  6   7  3

How can I set a filter to get only rows where the Md has the value B如何设置过滤器以仅获取Md值为B的行

I tried via field name我尝试通过字段名称

df[df['Md'] == 'B']

This raises a KeyError, because the "field" is not column, but part of the MultiIndex这会引发 KeyError,因为“字段”不是列,而是 MultiIndex 的一部分

df[df.index.map(lambda x: x[1] == 'B')]

works, but is very hacky.有效,但非常hacky。

Is there a shorter way to write this filter, maybe using the field name Md or the index position 1 ?是否有更短的方法来编写此过滤器,可能使用字段名称Md或索引 position 1

You can use DataFrame.query您可以使用DataFrame.query

df.query("Md == 'B'")

Output Output

          O  I
Hi Md Lo      
A  B  C   7  2
      C   7  1

Alternative (Might not work with different data)替代方案(可能不适用于不同的数据)

df.filter(like='B', axis=0)

Here is a way by passing a tuple in to df.loc这是一种将元组传递给df.loc的方法

df.loc[(slice(None),'B',slice(None))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM