计算Pandas群体中的计数百分比

f4t66c6m  于 2022-11-27  发布在  其他
关注(0)|答案(2)|浏览(121)

我想发现我的特征和目标之间的潜在模式,所以我尝试使用groupby,但我想计算的不是计数,而是每个类的计数占总数的比率或百分比,下面的代码与我所做的工作类似。

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'])
pexxcrt2

pexxcrt21#

您可以通过以下方式更简单地实现这一点:

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

输出量:

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

如果您希望多索引的顺序相同:

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

输出量:

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
i2loujxw

i2loujxw2#

我是这样做的

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

相关问题