'subseting a dataframe excluding top20 and lowest20

I am sampling a dataframe to get 3 new dataframes. The first 2 I use this code:

# Get Top 20 
top20 = df['column'].value_counts().keys()[:20]

# Get Lowest 20
low20 = df['column'].value_counts().keys()[-20:]

What I want:

How can I get the same daframe excluding the top 20 and low 20, I mean getting what's between top 20 and low 20?



Solution 1:[1]

Try this:

between = df['column'].value_counts().keys()[20:-20]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 richardec