0

I want to find out the percentage of occurrence particular group(group by 2 columns) with the no of occurrence of group by of single column. Like in below i want to calculate the occurence of group by(Cardno,Sitedesc) divided by the group by(Cardno). Thanks in advance

Data

         Cardno  Sitedesc
0        883484        30
1       1017011        59
2        304531        48
3        304531        59
4        304531        32
5        687253        46
6        351841        59
7        976365        58
8        983485        46
9        326465        30
10       326465        40
11       983485        58
12       983485        46
13       983485        48
14       847535        31
15       687250        47
16       687250        46

1 Answers1

0

Use:

s = df.groupby(['Cardno','Sitedesc']).size()
out = s.div(s.sum(level=0), level=0)
print (out)

Cardno   Sitedesc
304531   48          1.0
326465   30          1.0
351841   59          1.0
395354   59          1.0
687253   46          1.0
687463   59          1.0
847535   31          1.0
883484   30          1.0
976365   58          1.0
983485   46          1.0
1017011  59          1.0
1038308  58          1.0
dtype: float64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252