简体   繁体   中英

How to filter multi-index dataframe by column with multiple levels?

Having the following pandas dataframe, I like to reduce the dataframe to just include columns ('count','LARGE') and ('50%','LARGE') and also all LabelX columns.

pd.DataFrame({('Label1', ''): {2363: 'D2',
2375: 'D2',
2387: 'D2',
2783: 'D2'},
('Label2', ''): {2363: 'D3',
2375: 'D3',
2387: 'D3',
2783: 'D3'},
('Label3', ''): {2363: 'D4',
2375: 'D4',
2387: 'D4',
2783: 'D4'},
('Label4', ''): {2363: 'na',
2375: 'na',
2387: 'na',
2783: 'na'},
('Label5', ''): {2363: 'False',
2375: 'False',
2387: 'False',
2783: 'False'},
('Label6', ''): {2363: 'D5',
2375: 'D5',
2387: 'D5',
2783: 'D5'},
('Label7', ''): {2363: 'A S',
2375: 'B S',
2387: 'C C',
2783: 'W I'},
('count', 'LARGE'): {2363: 777.0,
2375: 777.0,
2387: 777.0,
2783: 777.0},
          ('50%', 'LARGE'): {2363: pd.Timedelta('0 days 00:00:20'),
2375: pd.Timedelta('0 days 00:15:53'),
2387: pd.Timedelta('0 days 00:16:00'),
2783: pd.Timedelta('0 days 00:01:04')},
          ('50%', 'MEDIUM'): {2363: pd.Timedelta('0 days 00:00:20'),
2375: pd.Timedelta('0 days 00:12:49'),
2387: pd.Timedelta('0 days 00:13:54'),
2783: pd.Timedelta('0 days 00:01:01')},
         }
        )

Already tried the approach dropping columns with:

.drop(columns=[('count','LARGE'),('count','SMALL')])

What I would like to know is if I can specify what to keep instead of dropping not needed columns. My use case has a lot of columns then dropping requires more code...

Example of expected output:

pd.DataFrame({('Label1', ''): {2363: 'D2',
2375: 'D2',
2387: 'D2',
2783: 'D2'},
('Label2', ''): {2363: 'D3',
2375: 'D3',
2387: 'D3',
2783: 'D3'},
('Label3', ''): {2363: 'D4',
2375: 'D4',
2387: 'D4',
2783: 'D4'},
('Label4', ''): {2363: 'na',
2375: 'na',
2387: 'na',
2783: 'na'},
('Label5', ''): {2363: 'False',
2375: 'False',
2387: 'False',
2783: 'False'},
('Label6', ''): {2363: 'D5',
2375: 'D5',
2387: 'D5',
2783: 'D5'},
('Label7', ''): {2363: 'A S',
2375: 'B S',
2387: 'C C',
2783: 'W I'},
('count', 'LARGE'): {2363: 777.0,
2375: 777.0,
2387: 777.0,
2783: 777.0},
          ('50%', 'LARGE'): {2363: pd.Timedelta('0 days 00:00:20'),
2375: pd.Timedelta('0 days 00:15:53'),
2387: pd.Timedelta('0 days 00:16:00'),
2783: pd.Timedelta('0 days 00:01:04')}
         }
        )

您可以使用布尔索引保留 level1 等于 '' 或 'LARGE' 的列:

df.loc[:, df.columns.get_level_values(1).isin(['', 'LARGE'])]

一种模式filter

df.filter(like='L')

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