How to select columns based on true/false condition in pandas

Viewed 7564

I want to select columns where the following is true.

mask = df.select_dtypes(include = ['object']).apply(pd.Series.nunique) > 100

rbc      False
pc       False
pcc      False
ba       False
htn      False
dm       False
cad      False
appet    False
pe       False
ane      False
class    False
lev_a     True
lev_b     True
l_x      False
dtype: bool
2 Answers

Use the result as a boolean mask

df.loc[:, mask]

The problem is likely that the number of rows in your mask is different than the number of columns in your original dataframe (assuming your columns are a mix of object and non-object).

Given your set up, this should return the columns satisfying your criterion (which you can then use as an input for loc[]):

mask.index[mask]

You want to say mask.index here rather than mask.columns because your mask is a pandas series where the index consists of the columns in your original dataframe df.

Here's a more complete example with sample data (in your future questions please provide your own sample data and you'll generally get more and better answers):

df = pd.DataFrame( np.random.choice(list('abcde'),[10,5]), 
                          columns=list('ABCDE'))

mask = df.select_dtypes(include=['object']).apply(pd.Series.nunique) > 4

mask.index[mask]

Result:

Index(['B', 'C'], dtype='object')
Related