anilbey
anilbey

Reputation: 1977

Pandas multiindex dataframe selecting data

Given this dataframe below,
Is there a way to retrieve the columns having start values >= 20000 and stop values <= 40000? Where start and stop are the names (not values) of the columns.

Multi-index dataframe

Upvotes: 2

Views: 69

Answers (1)

jezrael
jezrael

Reputation: 862406

I think you can use get_level_values + astype to int (if necessary), create mask with & and last select by loc:

mask1 = df.columns.get_level_values('start').astype(int) >= 20000
mask2 = df.columns.get_level_values('stop').astype(int) <= 40000

mask = mask1 & mask2
df = df.loc[:, mask]
print (df)

Upvotes: 5

Related Questions