Initialize all-true boolean index for Pandas


I find myself sometimes building a boolean/mask iteratively, so something like:

mask = initialize_mask_to_true()
for condition in conditions:
  mask = mask & condition

df_masked = pd.loc[mask, my_cols]

Where conditions might be a list of separate boolean masks or comparisons like df[some_col] > someVal
Is there a good way to do the initialize_mask_to_true()? Sometimes I’ll do something that feels ugly like:

mask = ~(df.loc[:, df.columns[0]] == np.nan)

which works because something == np.nan will always be false, but it feels like there’s a cleaner way.

Asked By: Andrew



I use numpy.ones for that:

np.ones(df.shape[0], dtype=bool)
Answered By: koPytok

If the index must be preserved:

mask= pd.DataFrame(True,index=df.index,columns=df.columns)


mask= pd.DataFrame(True,index=df.index,columns=[df.columns[0]])
Answered By: kantal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.