我正在计算每年的负数和正数。最终,我希望获得每年的负面和正面百分比。

我尝试了groupby年和计数类别,但新列显示没有名称。

    df1= df.groupby(['Year','Count of Negative/Positive Margins'])['Count of Negative/Positive Margins'].count()

    df1.head()
    Out[194]:
    Year  Count of Negative/Positive Margins
    2005  1                                     4001
          2                                     1373
    2006  1                                     4046
          2                                     1304
    2007  1                                     4156
    Name: Count of Negative/Positive Margins, dtype: int64


这是我的预期输出:

    2005  1                                     74%
          2                                     26%
    .
    .
    .

最佳答案

SeriesGroupBy.value_counts与仅对列Year和参数normalize=True进行分组一起使用,然后乘以100,再乘以Series.round,转换为字符串并添加%

df = (df.groupby('Year')['Count of Negative/Positive Margins']
        .value_counts(normalize=True)
        .mul(100)
        .round()
        .astype(str)
        .add('%')
        .reset_index(name='percentage')
       )

关于python - 计算年份子集中的类别,然后除以该子集中的总数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55588240/

10-12 19:10