python - 如何从数据框列中的值列表中获取值
问题描述
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 中提取值并将其转换为所需的电子邮件格式?
解决方案
您使任务更难连接字符串以生成消息,然后尝试拆分消息。您可以简单地将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
推荐阅读
- c++ - 在 Windows 上测量 c++ 程序的执行时间
- metaprogramming - TextGen 从一个概念生成不同的编程语言代码
- asp.net - 是否可以使用 dotNet 使用 Hangfire 每 20 秒运行一次作业?
- javascript - 如何在 FullCalendar 的每一行之前添加自定义 HTML?
- php - PhpUnit 无权访问公共目录
- html - 隐藏网格列,其余列重新分配
- angular - 是否可以根据条件从抽象模型中忽略/替换验证器?
- c++ - 通过基类指针进行奇怪的模板类转换
- javascript - 更改悬停点上标记的颜色 - Highcharts
- html - 我们与 AdSense html 代码有什么关系