简体   繁体   English

计算 pandas groupby 中计数的百分比

[英]calculating the percentage of count in pandas groupby

I want to discover the underlying pattern between my features and target so I tried to use groupby but instead of the count I want to calculate the ratio or the percentage compared to the total of the count of each class the following code is similar to the work I have done.我想发现我的特征和目标之间的潜在模式,所以我尝试使用 groupby 但我想计算的不是计数,而是与每个 class 的总计数相比的比率或百分比,以下代码类似于工作我已经做好了。

fet1=["A","B","C"]
fet2=["X","Y","Z"]
target=["0","1"]
df = pd.DataFrame(data={"fet1":np.random.choice(fet1,1000),"fet2":np.random.choice(fet2,1000),"class":np.random.choice(target,1000)})
df.groupby(['fet1','fet2','class'])['class'].agg(['count'])

You can achieve this more simply with:您可以通过以下方式更简单地实现此目的:

out = df.groupby('class').value_counts(normalize=True).mul(100)

Output: Output:

class  fet1  fet2
0      A     Y       13.859275
       B     Y       12.366738
             X       12.153518
       C     X       11.513859
             Y       10.660981
       B     Z       10.447761
       A     Z       10.021322
       C     Z        9.594883
       A     X        9.381663
1      A     Y       14.124294
       C     Z       13.935970
       B     Z       11.676083
             Y       11.111111
       C     Y       11.111111
             X       11.111111
       A     X       10.169492
       B     X        9.416196
       A     Z        7.344633
dtype: float64

If you want the same order of multiindex:如果你想要相同顺序的多索引:

out = (df
 .groupby('class').value_counts(normalize=True).mul(100)
 .reorder_levels(['fet1', 'fet2', 'class']).sort_index()
)

Output: Output:

fet1  fet2  class
A     X     0         9.381663
            1        10.169492
      Y     0        13.859275
            1        14.124294
      Z     0        10.021322
            1         7.344633
B     X     0        12.153518
            1         9.416196
      Y     0        12.366738
            1        11.111111
      Z     0        10.447761
            1        11.676083
C     X     0        11.513859
            1        11.111111
      Y     0        10.660981
            1        11.111111
      Z     0         9.594883
            1        13.935970
dtype: float64

I achieved it by doing this我通过这样做实现了它

fet1=["A","B","C"]
fet2=["X","Y","Z"]
target=["0","1"]
df = pd.DataFrame(data={"fet1":np.random.choice(fet1,1000),"fet2":np.random.choice(fet2,1000),"class":np.random.choice(target,1000)})
df.groupby(['fet1','fet2','class'])['class'].agg(['count'])/df.groupby(['class'])['class'].agg(['count'])*100

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

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