简体   繁体   中英

How do I get dataframe values with multiindex where some value is NOT in multiindex?

Here is example of my df (for example):

                    2000-02-01               2000-03-01             ...
                    sub_col_one sub_col_two sub_col_one sub_col_two ...
   idx_one  idx_two                             
    2       a       5           2           3           3           
    0       b       0           5           8           1           
    2       x       0           0           6           1           
    0       d       8           3           5           5       
    3       x       5           6           5           9           
    2       e       2           5           0           5           
    3       x       1           7           4           4

The question: How could I get all rows of that df, where idx_two is not equal to x ?

I've tried get_level_values , but cant get what I need.

Use Index.get_level_values with name of level with boolean indexing :

df1 = df[df.index.get_level_values('idx_two') != 'x']

Or with position of level, here 1 , because python counts from 0 :

df1 = df[df.index.get_level_values(1) != 'x']

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