首页 > 解决方案 > 如何从数据框列中的值列表中获取值

问题描述

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':'xxx@gmail.com','email_msg':message}
df = pd.DataFrame.from_dict([mail_dict])
mail_df = mail_df.append(df)

上面的代码将在循环中执行,并将在数据框中添加更多行,下面是最终的数据框: 在此处输入图像描述

然后我使用了基于 email_id 的 groupby,代码和输出数据框如下:

grouped = mail_df.groupby('email_id').aggregate(lambda x: x.tolist())
pd.set_option("max_colwidth",2)
print(grouped)

输出 在此处输入图像描述

分组数据框输出:

                                                                                                                                                                email_msg
email_id
xxx@gmail.com  [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]
yyy@gmail.com  [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] 

从分组的数据框中,我需要根据分组的数据框将邮件发送到 email_id 列中的相应 ID,并且消息应采用以下格式:

Dear X,
The below are the account status:

 - Active -> January
 - Active -> February
 - Inactive -> March

以类似的方式,电子邮件必须发送给其他用户。 谁能帮我处理分组数据框中每行的 email_msg 列中的值列表,并从 email_msg 中提取值并将其转换为所需的电子邮件格式?

标签: pythonpandasdataframe

解决方案


您使任务更难连接字符串以生成消息,然后尝试拆分消息。您可以简单地将name,action和添加month到数据框。

您还可以通过创建一个 dict 列表然后使用它创建数据框来简化代码。而不是每次都创建一个新的 Dataframe 并将其附加到mail_df.

这是一个解决方案

import pandas as pd


list_of_emails = []
name = 'X'
action = 'active'
month = 'January'
mail_dict = {'email_id': 'xxx@gmail.com', 'name': name, "action": action, "month": month}
list_of_emails.append(mail_dict)

mail_df = pd.DataFrame(list_of_emails)
grouped = mail_df.groupby('email_id').aggregate(lambda x: x.tolist())

for index, row in grouped.iterrows():
    print(index)  # your email id for this email

    # Format the email message
    # we can take the first element of the list since the name should be the same
    message = f"Dear {row['name'][0]},\n"

    for i in range(len(row["action"])):
        message = message + f" - {row['action'][i]} -> {row['month'][i]} \n"


    print(message)
    # send the email

输出消息:

Dear X,
 - active -> January 

推荐阅读