user16286965
user16286965

Reputation:

pandas dataframe groupby and then apply function

I'd like to calculate the percentage of passing the level on everyday and every level

example dataset likes the following

date pass the level level no count
2021-05-05 true 1 10
2021-05-05 false 1 3
2021-05-05 true 2 8
2021-05-10 true 2 5
2021-05-10 false 2 9

and the outcome I want looks

date level no passing rate
2021-05-05 1 0.7692

Upvotes: 0

Views: 60

Answers (1)

le_camerone
le_camerone

Reputation: 630

df = pd.DataFrame(df.values.repeat(df.count, axis=0), columns=df.columns)


df.groupby(['date','level no'])['pass the level'].astype(int).mean()

You might first have to convert the boolean column strings so that the first letteer is a capital:

df['pass the level'] = df['pass the level'].str.upper()

Upvotes: 1

Related Questions