-1

I have what I believe is a simple question but I can't find what I'm looking for in the docs.

I have a dataframe with a Categorical column called mycol with categories a and b and would like to be mask a subset of the dataframe as follows:

df_a = df[df.mycol.equal('a')]

Currently I am doing:

df_a = df[df.mycol.cat.codes.values==df.mycol.cat.categories.to_list().index('a')]

which is obviously extremely verbose and inelegant. Since df.mycol has both the codes and the coded labels, it has all the information to perform this operation, so I'm wondering the best way to go about this...

user27886
  • 540
  • 12
  • 27

1 Answers1

1
df_a = df[df["mycol"]=='a'] 

I believe this should work, unless by 'mask' you mean you want to actually zero out the values that don't have a

user3772547
  • 165
  • 10