2

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

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.