【问题标题】:counting for specific value in pandas .value_counts()计算 pandas .value_counts() 中的特定值
【发布时间】:2020-10-13 03:34:14
【问题描述】:

我目前有一列包含两个性别值,男性 = 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') 

【问题讨论】:

    标签: python pandas jupyter-notebook


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-29
      • 2021-12-13
      • 2022-12-04
      • 2016-06-02
      • 1970-01-01
      • 2020-12-20
      相关资源
      最近更新 更多