I am working with some advertising data, such as email data. I have two data sets:
one at the mail level, that for each person, states what days they were mailed, and then what day they were converted.
import pandas as pd df_emailed=pd.DataFrame() df_emailed['person']=['A','A','A','A','B','B','B'] df_emailed['day']=[2,4,8,9,1,2,5] df_emailed print(df_emailed) person day 0 A 2 1 A 4 2 A 8 3 A 9 4 B 1 5 B 2 6 B 5I have a summary dataframe that says whether someone converted, and which day they converted.
df_summary=pd.DataFrame() df_summary['person']=['A','B'] df_summary['days_max']=[10,5] df_summary['convert']=[1,0] print(df_summary) person days_max convert 0 A 10 1 1 B 5 0
I would like to combine these into a final dataframe that says, for each person:
- 1 to max date,
- whether they were emailed (0,1) and on the last day in the dataframe,
- whether they converted or not (0,1).
We are assuming they convert on the last day in the dataframe.
I know to do to this using a nested for loop, but I think that is just incredibly inefficient and sort of dumb. Does anyone know an efficient way of getting this done?
Desired result
df_final=pd.DataFrame()
df_final['person']=['A','A','A','A','A','A','A','A','A','A','B','B','B','B','B']
df_final['day']=[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5]
df_final['emailed']=[0,1,0,1,0,0,0,1,1,0,1,1,0,0,1]
df_final['convert']=[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0]
print(df_final)
person day emailed convert
0 A 1 0 0
1 A 2 1 0
2 A 3 0 0
3 A 4 1 0
4 A 5 0 0
5 A 6 0 0
6 A 7 0 0
7 A 8 1 0
8 A 9 1 0
9 A 10 0 1
10 B 1 1 0
11 B 2 1 0
12 B 3 0 0
13 B 4 0 0
14 B 5 1 0
Thank you and happy holidays!