Pandas newbie trying to replicate sql to python. Referencing the below post, I could use a simple function to calculate the weighted average of a column in a pandas dataframe.
Calculate weighted average using a pandas/dataframe
Date ID wt value
01/01/2012 100 0.50 60
01/01/2012 101 0.75
01/01/2012 102 1.00 100
01/02/2012 201 0.50
01/02/2012 202 1.00 80
However, if I had conditions in both numerator and denominator and to get an aggregate of the weighted average, I would do the below in sql:
SELECT
date
, id
, SUM(CASE WHEN value IS NOT NULL THEN value * wt ELSE 0 END) /
NULLIF(SUM(CASE WHEN value > 0 THEN wt ELSE 0 END), 0)
AS wt_avg
FROM table
GROUP BY date, id
How would we replicate this in Pandas?
Thanks in advance.