为什么Pandas中的value_count方法返回零计数?

时间:2017-10-15 06:31:00

标签: python pandas

我有一个数据框,用于存储高中学生注册的大学。

 School     Student_id 
   A             111
   A             112
   B             223

我正在使用value_counts()函数查看独特学院的列表以及每所大学注册的学生人数。但是,该列表包含了不少有0个计数的大学。为什么会这样?难道整个价值计数概念是围绕着学校必须出现在要计算的数据集中的事实构建的吗?我在这里缺少什么?

dtype是字符串。

代码:

ncee_sample_2005.clg.nunique() # Numer of unique colleges
49
ncee_sample_2005.clg.value_counts() # Visually check the college list
哈尔滨工业大学           9
吉林大学                 7
哈尔滨工程大学           7
浙江大学城市学院          0
浙江大学                 0

1 个答案:

答案 0 :(得分:1)

如果列是分类的,是否可以,因为添加了缺失的类别0 - 请检查here

print (df['Student_id'].dtype)
category

print (df['Student_id'])
0    111
1    112
2    223
Name: Student_id, dtype: category
Categories (4, int64): [111, 112, 223, 100]

s = df['Student_id'].value_counts()
print (s)
223    1
112    1
111    1
100    0
Name: Student_id, dtype: int64