My input dataframe name 'df'
I want to create a new dataframe name 'df1' group by 'date' in df, which include 2 fields: 'date' and 'sponsorRate' with calculation formula as below
date = df.date sponsorRate = count(sponsored) on check column / count total on check column
My desired output
Tks for all helps
CodePudding user response:
Compare check
and aggregate mean
:
df1 = df['check'].eq('sponsored').groupby('date').mean().reset_index()
Or:
df1 = df['check'].eq('sponsored').groupby(level=0).mean().reset_index()