具有以下pandas数据框,我希望将数据框缩小为仅包含列('count','LARGE')和('50%','LARGE')以及所有LabelX列。
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')},
}
)
已经尝试使用以下方法删除列:
.drop(columns=[('count','LARGE'),('count','SMALL')])
我想知道的是,我是否可以指定要保留的内容而不是删除不需要的列。我的用例有很多列,然后删除需要更多代码...
预期输出示例:
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')}
}
)
答案 0 :(得分:1)
您可以使用布尔值索引来保留level1等于''或'LARGE'的列:
df.loc[:, df.columns.get_level_values(1).isin(['', 'LARGE'])]
答案 1 :(得分:0)
一种模式filter
df.filter(like='L')