python - How to get percentage of counts of a column after groupby in Pandas -


i'm trying distribution of grades each rank names in list of data. however, can't figure out how proportion/percentage of each grade count on rank group. here's example:

df.head()

name    rank    grade bob     1       bob     1       bob     1       b bob     1       c bob     2       b bob     3       c joe     1       c joe     2       b joe     2       b joe     3       joe     3       b joe     3       b 

i use grade_count = df.groupby(['name', 'rank', 'grade']).['grade'].size()) give me count of each grade within (name,rank) group:

name    rank    grade bob     1           2                 b     1                 c     1         2       b     1         3       c     1 joe     1       c     1         2       b     2         3           1                 b     2 

now each size calculated, i'd proportion (name,rank) group (i.e. proportion of grade within rank, within system) this output i'd like:

name    rank    grade bob     1           2    0.5   (bob @ rank 1 had 4 grades, , 50% of them a's)                 b     1    0.25                 c     1    0.25         2       b     1    1         3       c     1    1 joe     1       c     1    1         2       b     2    1         3           1    0.33                 b     2    0.66 

i've managed totals of each rank group using rank_totals = grade_count.groupby(level[0,1]).sum() results in:

name    rank     bob     1       4         2       1         3       1 joe     1       1         2       2         3       3 

how can divide numbers grade_count corresponding rank totals in rank_totals?

group data name , rank levels, , use transform total of series , broadcast entire series. use series divide current one:

grade_count.groupby(level = [0,1]).transform(sum) out[19]:  name  rank  grade bob   1            4             b        4             c        4       2     b        1       3     c        1 joe   1     c        1       2     b        2       3            3             b        3 dtype: int64  grade_count / grade_count.groupby(level = [0,1]).transform(sum) out[20]:  name  rank  grade bob   1            0.500000             b        0.250000             c        0.250000       2     b        1.000000       3     c        1.000000 joe   1     c        1.000000       2     b        1.000000       3            0.333333             b        0.666667 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -