python - pandas syntax for more complex sql query -
i looking pandas syntax following aggregation pandas dataframe. cannot find example how accomplish following sql query in pandas.
#sum , divide select click, ctr, sum(click)/sum(imp) ctr mytable group website #normalize each subgroup select imp, imp/sum(imp) on (partition website) n_imp mytable
sql:
#normalize each subgroup select imp, imp/sum(imp) on (partition website) n_imp mytable
pandas:
df[['website','imp']].assign(n_imp=df['imp']/df.groupby('website')['imp'].transform('sum'))
Comments
Post a Comment