简体   繁体   中英

How can I filter a pandas dataframe within a nested column set?

I have the following pandas dataframe:

+---+-------------+-------------+
|   | Col1        |             |
+   +-------------+-------------+
|   | Sub1 | Sub2 | SubX | SubY |
+---+------+------+------+------+
| 0 | N    | A    | 1    | Z    |
| 1 | N    | B    | 1    | Z    |
| 2 | N    | C    | 2    | Z    |
| 3 | N    | D    | 2    | Z    |
| 4 | N    | E    | 3    | Z    |
| 5 | N    | F    | 3    | Z    |
| 6 | N    | G    | 4    | Z    |
| 7 | N    | H    | 4    | Z    |
+---+------+------+------+------+

I would like to filter the dataframe by column SubX , the selected rows should have the value 3 , like this:

+---+-------------+-------------+
|   | Col1        |             |
+   +-------------+-------------+
|   | Sub1 | Sub2 | SubX | SubY |
+---+------+------+------+------+
| 4 | N    | E    | 3    | Z    |
| 5 | N    | F    | 3    | Z    |
+---+------+------+------+------+

Could you help to find the right pandas query? It's pretty hard for me, because of the nested column structure. Thanks a lot!

I extended multiindex hierarchie because it wasn't clear to me what the blank space should be.

df

    Col1            Col2
    Sub1    Sub2    SubX    SubY
0   N       A       1       Z
1   N       B       1       Z
2   N       C       2       Z
3   N       D       2       Z
4   N       E       3       Z
5   N       F       3       Z
6   N       G       4       Z
7   N       H       4       Z

Now do the following:

df[df['Col2','SubX']==3]

Output

    Col1            Col2
    Sub1    Sub2    SubX    SubY
4   N       E       3       Z
5   N       F       3       Z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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