i'm preatty beginner using python, i'm trying to calculate the open rate ratio (a ration between two distinct count) in just one code line. My dataframe is like this:
df = pd.DataFrame([
(142, 1, 'open' , 'Mobile'),
(144, 2, 'open' , 'Mobile'),
(144, 1, 'delivered', 'Web'),
(142, 1, 'delivered', 'Mobile'),
(142, 2, 'delivered', 'Web'),
(144, 1, 'open', 'Web'),
(142, 2, 'open', 'Mobile')
], columns=['sent_mail_id', 'customer_id', 'event' , 'Tool_used'])
I would like to calculate the open rate while grouping by the Column Tool_used Using Pandas. In SQL Language would be this:
select
Tool_used ,
count(distinct case when event='open' then sent_mail_id end)/count(distinct case when
event='delivered' then sent_mail_id end)
from df
group by 1
Note that i would need to count distinctly the sent_mail_id since unique count is needed. Thank you