How to select rows based categories in Pandas dataframe

◇◆丶佛笑我妖孽 提交于 2019-12-21 21:39:56

问题


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

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?


回答1:


This works:

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

UPDATE

The question was updated. New solution:

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



回答2:


You can query the categorical list using df.cats.cat.categories which prints output as

Index(['a', 'b'], dtype='object')

For this case, to select a row with category of 'a' which is df.cats.cat.categories['0'], you just use:

df[df.cats == df.cats.cat.categories[0]]



回答3:


df[df.cats.cat.categories == df.cats.cat.categories[0]]


来源:https://stackoverflow.com/questions/33468566/how-to-select-rows-based-categories-in-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!