Home > Net >  calculating the percentage of count in pandas groupby
calculating the percentage of count in pandas groupby

Time:11-21

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.

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

CodePudding user response:

You can achieve this more simply with:

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

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:

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

CodePudding user response:

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

  • Related