繁体   English   中英

计算熊猫中的特定值 .value_counts()

[英]counting for specific value in pandas .value_counts()

我目前有一列包含两个性别值,男性 = 1,女性 = 2,我想计算并显示下一个性别的值。

Gender = [1, 1, 1, 2, 1, 2, 2, 2, 1, 1, 2]

当我使用此代码时

df['Gender'].value_counts() 

我得到以下输出:

1 6
2 5

Gender dType is int64

但我正在寻找以下结果。 但是,我一直得到与以下不同的输出:-

The total number of Male: 6
The total number of Female: 5

这是我使用的打印,但它一直给我一个错误。 我想我可能需要你的帮助来解决这个问题。 先感谢您。

print('The total number of Male:' df[Gender].value_counts(),'\n') 

value_counts 返回系列。 因此,您必须通过使用它所在的位置来访问特定值,您可以尝试以下操作:

输入 1:

Gender = [1, 1, 1, 2, 1, 2, 2, 2, 1, 1, 2]
series = df['Gender'].value_counts() 
print(series)

输出 1:

1    6
2    5
dtype: int64

输入:2

print('The total number of Males : ', series[1])
print('The total number of Female : ', series[2]

输出 2

The total number of Males : 6
The total number of Females : 5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM