熊猫分组两列

时间:2017-06-15 17:52:47

标签: python pandas

我的数据如下所示:

     Gender    topic: Big data infrastructure
    0     F     NaN
    1     M     -1
    2     M     -1
    3     M     -1
    4     F     1
    5     M     NaN
    6     M     NaN
    7     M     NaN
    8     M     -2
    9     M     1
    10    F     1
    11    M     NaN
    12    M     1
    13    M     -1
    14    M     1
    15    M     NaN
    16    M     NaN
    17    M     NaN
    18    M     -1
    19    M     -2
    20    F     1
    21    M     NaN
    22    M     NaN
    23    F     2
    24    M     -2
    25    F     2
    26    M     NaN
    27    M     2
    28    M     1
    29    M     NaN
    30    M     2
    31    M     NaN
    32    M     NaN
    33    F     2
    34    M     2

我想以某种方式计算出有多少男性和女性得分为-2,-1,0,1,2或没有回答,但我无法弄明白。我尝试了几种groupby方法,但它们不起作用。有没有人有一些指导或提示?

1 个答案:

答案 0 :(得分:2)

执行groupby并使用value_counts

df.groupby('Gender')['topic: Big data infrastructure'].value_counts(dropna=False)

结果输出:

Gender  topic: Big data infrastructure
F        1.0                               3
         2.0                               3
        NaN                                1
M       NaN                               13
        -1.0                               5
         1.0                               4
        -2.0                               3
         2.0                               3