mail_df = pd.DataFrame()
name = 'X'
action = 'active'
month = 'January'
message = name + ' gmail account is ' + action + ' in the month of ' + month
mail_dict = {'email_id':'[email protected]','email_msg':message}
df = pd.DataFrame.from_dict([mail_dict])
mail_df = mail_df.append(df)
the above code will be performed in the loop and will be adding more rows into the dataframe, below is the final dataframe:

then I had used groupby based on email_id, and the code and output dataframe is below:
grouped = mail_df.groupby('email_id').aggregate(lambda x: x.tolist())
pd.set_option("max_colwidth",2)
print(grouped)
Grouped Dataframe Output:
email_msg
email_id
[email protected] [X gmail account is active in the month of January, X gmail account is active in the month of February, X gmail account is inactive in the month of March]
[email protected] [Y gmail account is active in the month of April, Y gmail account is inactive in the month of May, Y gmail account is inactive in the month of June]
from the grouped dataframe, I need to send mail to the respective ids in the email_id column as per the grouped dataframe , and the message should be in the below format:
Dear X,
The below are the account status:
- Active -> January
- Active -> February
- Inactive -> March
In the similar manner, email has to be sent to other users. Can anyone help me to process the list of values in the email_msg column in the grouped dataframe for each row, and extract the value from email_msg and make it into the required email format?
