Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select rows based categories in Pandas dataframe

this is really trivial but can't believe I have wandered around for an hour and still can find the answer, so here you are:

    df = pd.DataFrame({"cats":["a","b"], "vals":[1,2]})
    df.cats = df.cats.astype("category")
    df

df looks like this

My problem is how to select the row that its "cats" columns's category is "a". I know that df.loc[df.cats == "a"] will work but it's based on equality on element. Is there a way to select based on levels of category?

like image 688
Skywalker326 Avatar asked Nov 01 '15 23:11

Skywalker326


Video Answer


1 Answers

This works:

df.cats[df.cats=='a']

UPDATE

The question was updated. New solution:

df[df.cats.cat.categories == ['a']]
like image 92
Mike Müller Avatar answered Oct 25 '22 14:10

Mike Müller