问题
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