简体   繁体   中英

Percentage based on column value

I am trying to find best most efficent method to calculate the percentage that each teacher is not tenured where they are teaching in my df.

For example df below:

District   | Teacher Name | Tenured?
  55         Bo Carns         Yes
  42         Bo Carns         No
  55         Steven Ast       No
  43         Fiona Tan        Yes
  43         Steven Ast       Yes
  43         Mike Po          No
  31         Steve Chi        No

Each teacher can teach in multiple districts but they can be tenured or not tenured so I wanted to calculate the % that all the teachers in my df are not tenured to find the teachers teaching that are not tenured the most ( so for each teacher, the # of times the tenured column is no / all rows for the df )

Expected output would be:

Teacher Name | pct
Bo Carns       .5
Steven Ast     .5
Fiona Tan      0
Mike Po        1
Steve Chi      1

where the pct is the percent they were not tenured for all records or all districts

thanks for taking time to look at my question

You can try

s = df['Tenured?'].eq('Yes').groupby(df.TeacherName).mean()
Out[57]: 
TeacherName
BoCarns      0.5
FionaTan     1.0
MikePo       0.0
SteveChi     0.0
StevenAst    0.5
Name: Tenured?, dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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