如何在python中分类变量中计数频率

时间:2018-07-05 15:35:27

标签: python

我有问题,我正在尝试计算分类数据的频率:

例如,如果我得到:

enter image description here

我想得到

enter image description here

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

使用以下示例数据框(最小,但基于您的图像):

>>> df
    Data Visualization     Machine Learning           Statistics
0       Not Interested  Somewhat Interested      Very Interested
1       Not Interested       Not Interested  Somewhat Interested
2  Somewhat Interested       Not Interested      Very Interested
3  Somewhat Interested       Not Interested  Somewhat Interested
4      Very Interested      Very Interested       Not Interested

您可以使用value_counts

df.apply(lambda x: x.value_counts()).T


                    Not Interested  Somewhat Interested  Very Interested
Data Visualization               2                    2                1
Machine Learning                 3                    1                1
Statistics                       1                    2                2