<p>OP提出了两个问题;一个是关于使用<code>pandas</code>检索数据集中的数据,另一个是关于发送电子邮件</p>
<p>这是第一个问题的答案</p>
<p>由于我不确定我是否完全按照你的意图来解释你的问题,我提供了两种可能的解决方案</p>
<h2>第一种解决方案</h2>
<p>对于每个操作<code>action</code>,对于每个主管<code>s</code>,向主管<code>s</code>发送一封电子邮件,其中所有行与操作<code>action</code>匹配</p>
<pre class="lang-py prettyprint-override"><code>import pandas as pd
df = pd.read_csv('emaildata.csv', header=0)
# ID Name SupervisorName ... Column5 Column6 Column7
# 0 123 Tim Jones Sam Burk ... data data data
# 1 124 Mark Smith Sam Burk ... data data data
# 2 125 Jill Jones Tim Jones ... data data data
# 3 126 Sam Harris Sam Burk ... data data data
# 4 127 Jana Lester Lisa Jones ... data data data
actions = set(df['Action'])
# {2, 3}
for action in actions:
dfa = df[df['Action'] == action]
email_partial_body = 'The below accounts have had the Action {} performed on them. Please contact us with any concerns.\n\n'.format(action) + str(dfa) + '\n\nRegards,\nCorporate Events'
for i,row in dfa.drop_duplicates(subset=['SupervisorEmail']).iterrows():
email_full_body = ''.join(['Dear ', row['SupervisorName'], ',\n\n', email_partial_body])
email_to = row['SupervisorEmail']
send_email(email_to, email_full_body)
</code></pre>
<p>使用伪函数<code>send_email(to, body)</code>进行测试,上述代码生成以下4封电子邮件:</p>
<pre><code>====EMAIL====
TO: sburk@somerandom.com
====
Dear Sam Burk,
The below accounts have had the Action 2 performed on them. Please contact us with any concerns.
ID Name SupervisorName SupervisorEmail Action Column5 Column6 Column7
0 123 Tim Jones Sam Burk sburk@somerandom.com 2 data data data
1 124 Mark Smith Sam Burk sburk@somerandom.com 2 data data data
2 125 Jill Jones Tim Jones tjones@somerandom.com 2 data data data
4 127 Jana Lester Lisa Jones ljones@somerandom.com 2 data data data
Regards,
Corporate Events
=============
====EMAIL====
TO: tjones@somerandom.com
====
Dear Tim Jones,
The below accounts have had the Action 2 performed on them. Please contact us with any concerns.
ID Name SupervisorName SupervisorEmail Action Column5 Column6 Column7
0 123 Tim Jones Sam Burk sburk@somerandom.com 2 data data data
1 124 Mark Smith Sam Burk sburk@somerandom.com 2 data data data
2 125 Jill Jones Tim Jones tjones@somerandom.com 2 data data data
4 127 Jana Lester Lisa Jones ljones@somerandom.com 2 data data data
Regards,
Corporate Events
=============
====EMAIL====
TO: ljones@somerandom.com
====
Dear Lisa Jones,
The below accounts have had the Action 2 performed on them. Please contact us with any concerns.
ID Name SupervisorName SupervisorEmail Action Column5 Column6 Column7
0 123 Tim Jones Sam Burk sburk@somerandom.com 2 data data data
1 124 Mark Smith Sam Burk sburk@somerandom.com 2 data data data
2 125 Jill Jones Tim Jones tjones@somerandom.com 2 data data data
4 127 Jana Lester Lisa Jones ljones@somerandom.com 2 data data data
Regards,
Corporate Events
=============
====EMAIL====
TO: sburk@somerandom.com
====
Dear Sam Burk,
The below accounts have had the Action 3 performed on them. Please contact us with any concerns.
ID Name SupervisorName SupervisorEmail Action Column5 Column6 Column7
3 126 Sam Harris Sam Burk sburk@somerandom.com 3 data data data
Regards,
Corporate Events
=============
</code></pre>
<h2>第二种解决方案</h2>
<p>将总体处理限制为操作值为2和的行,按主管电子邮件对这些行进行分区,并向每个不同的主管发送一封电子邮件,其中仅包含与之关联的行</p>
<p><em>此解释是由用户mklement0</em>建议的</p>
<pre class="lang-py prettyprint-override"><code>import pandas as pd
df = pd.read_csv('emaildata.csv', header=0)
action = 2
for supervisor_email_address, subdf in df.groupby(by='SupervisorEmail'):
supervisor_name = subdf['SupervisorName'].iloc[0]
email_body = ''.join(
['Dear ',
supervisor_name,
',\n\nThe below accounts have had the Action {} performed on them. Please contact us with any concerns.\n\n'.format(action),
str(subdf),
'\n\nRegards,\nCorporate Events'])
send_email(supervisor_email_address, email_body)
</code></pre>
<p>这将产生以下三封电子邮件:</p>
<pre><code>====EMAIL====
TO: ljones@somerandom.com
====
Dear Lisa Jones,
The below accounts have had the Action 2 performed on them. Please contact us with any concerns.
ID Name SupervisorName ... Column5 Column6 Column7
4 127 Jana Lester Lisa Jones ... data data data
[1 rows x 8 columns]
Regards,
Corporate Events
=============
====EMAIL====
TO: sburk@somerandom.com
====
Dear Sam Burk,
The below accounts have had the Action 2 performed on them. Please contact us with any concerns.
ID Name SupervisorName ... Column5 Column6 Column7
0 123 Tim Jones Sam Burk ... data data data
1 124 Mark Smith Sam Burk ... data data data
3 126 Sam Harris Sam Burk ... data data data
[3 rows x 8 columns]
Regards,
Corporate Events
=============
====EMAIL====
TO: tjones@somerandom.com
====
Dear Tim Jones,
The below accounts have had the Action 2 performed on them. Please contact us with any concerns.
ID Name SupervisorName ... Column5 Column6 Column7
2 125 Jill Jones Tim Jones ... data data data
[1 rows x 8 columns]
Regards,
Corporate Events
=============
</code></pre>