获取分类变量的类别列表(Python Pandas)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52404971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:02:31  来源:igfitidea点击:

Get a list of categories of categorical variable (Python Pandas)

pythonpandascategorical-data

提问by Mona

I have a pandas DataFrame with a column representing a categorical variable. How can I get a list of the categories? I tried .values on the column but that does not return the unique levels.

我有一个 Pandas DataFrame,其中有一列表示分类变量。如何获取类别列表?我在列上尝试了 .values ,但这不会返回唯一级别。

Thanks!

谢谢!

回答by jezrael

I believe need Series.cat.categoriesor unique:

我相信需要Series.cat.categoriesunique

np.random.seed(1245)

a = ['No', 'Yes', 'Maybe']
df = pd.DataFrame(np.random.choice(a, size=(10, 3)), columns=['Col1','Col2','Col3'])
df['Col1'] = pd.Categorical(df['Col1'])

print (df.dtypes)
Col1    category
Col2      object
Col3      object
dtype: object

print (df['Col1'].cat.categories)
Index(['Maybe', 'No', 'Yes'], dtype='object')

print (df['Col2'].unique())
['Yes' 'Maybe' 'No']

print (df['Col1'].unique())
[Maybe, No, Yes]
Categories (3, object): [Maybe, No, Yes]

回答by Anand Mohan

You can also use value_counts(), but it only works when you use it with a column name, with which you'll get the counts of each category as well. Example:

您也可以使用 value_counts(),但它仅在您将它与列名一起使用时才有效,您还将获得每个类别的计数。例子:

dataframe['Columnn name'].value_counts()

数据框['列名称'].value_counts()

Alternatively, if you want the total count of categories in a variable, you can do this,

或者,如果您想要变量中类别的总数,您可以这样做,

dataframe['Columnn name'].value_counts().count()

数据框['列名称'].value_counts().count()

回答by SABARISH KAVALA

Try executing the below code.

尝试执行以下代码。

List_Of_Categories_In_Column=list(df['Categorical Column Name'].value_counts().index)

List_Of_Categories_In_Column=list(df['Categorical Column Name'].value_counts().index)