为每个组按组提取数据

2024-09-27 00:13:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张Excel表格

participantData.xlsx

email      Name     Date        RegPushupCount    EasyPushupCount    DifficultPushupCount
a@b.com    John     2020-05-01         5                  0                   0
a@b.com    John     2020-05-01         5                  0                   0
a@b.com    John     2020-05-02         0                  5                   0
a@b.com    Jane     2020-05-02         5                  0                   0
a@b.com    Jane     2020-05-01         0                  0                   5
b@a.com    Bill     2020-05-01         0                  0                   5
b@a.com    Bill     2020-05-02         0                  5                   0

我想给每封邮件发一封邮件,总结他们做了多少俯卧撑。我有一个功能发送电子邮件(电子邮件地址,电子邮件文本),这将照顾电子邮件部分

我想发送这样的电子邮件

发送电子邮件至:a@b.com

John               Regular Push ups     Easy Push ups   Difficult Pushups
      2020-05-01          10               0                    0
      2020-05-02          0                5                    0
Jane               Regular Push ups     Easy Push ups   Difficult Pushups
      2020-05-01          5                0                    0
      2020-05-02          0                5                    0

发送电子邮件至:b@a.com

Bill               Regular Push ups     Easy Push ups   Difficult Pushups
      2020-05-01          10               0                    0
      2020-05-02          0                5                    0

我希望这能解释清楚

我已经走了多远:

participantsData = pd.DataFrame(
...       pushupCountXLS.groupby(['Email', 'Name', 'Date'] ).sum().astype(int))

请您指导我如何从数据框中提取每个电子邮件地址的数据,并向他们发送有关他们所做俯卧撑的电子邮件

以防万一,如果你想知道,一群美国朋友正在进行100天俯卧撑挑战,我们在谷歌表单中输入这些数据,这就像从谷歌表单发送给他们的确认电子邮件:)


Tags: 数据namecomdate电子邮件easy邮件john
2条回答

下面是我如何解决这个问题的,如果你有任何优化的想法,请不要犹豫发表评论。非常感谢。感谢@Mayank Porwal的帮助

participantsData = pushupCountXLS.groupby(['Email', 'Name', 'Date'], as_index=False ).agg(sum)
uniqueEmailAdd = participantsXLS['Email Address'].unique()
for em in uniqueEmailAdd:
    if check_valid_email(str(em)):
        temp = participantsData[participantsData['Email'].eq(em)]
        temp = temp.astype({'Regular':int, 'Easy':int, 'Special':int})
        uniqunames=(temp['Name'].unique())
        strtemp2=""
        for participant in uniqunames:
            #strtemp2 = temp[['Date', 'Regular', 'Easy', 'Special']].to_string(index=False)
            temp2 = temp[temp['Name'].eq(participant)]
            strtemp2 = str(participant) + '\n'
            strtemp2 += temp2[['Date', 'Regular', 'Easy', 'Special']].to_string(index=False)
        emailcounter += 1
        print(emailcounter)
        send_email(em, strtemp2)

^{}^{}一起使用:

In [1476]: df.groupby(['email','Name','Date']).agg('sum')
Out[1476]: 
                         RegPushupCount  EasyPushupCount  DifficultPushupCount
email   Name Date                                                             
a@b.com Jane 2020-05-01               0                0                     5
             2020-05-02               5                0                     0
        John 2020-05-01              10                0                     0
             2020-05-02               0                5                     0
b@a.com Bill 2020-05-01               0                0                     5
             2020-05-02               0                5                     0

OP评论后:

In [1566]: res = df.groupby(['email','Name','Date'], as_index=False).agg('sum')

您可以像这样获取a@b.com的所有记录:

In [1568]: res[res['email'].eq('a@b.com')]
Out[1568]: 
     email  Name        Date  RegPushupCount  EasyPushupCount  DifficultPushupCount
0  a@b.com  Jane  2020-05-01               0                0                     5
1  a@b.com  Jane  2020-05-02               5                0                     0
2  a@b.com  John  2020-05-01              10                0                     0
3  a@b.com  John  2020-05-02               0                5                     0

你可以得到你的所有记录b@a.com像这样:

In [1569]: res[res['email'].eq('b@a.com')]
Out[1569]: 
     email  Name        Date  RegPushupCount  EasyPushupCount  DifficultPushupCount
4  b@a.com  Bill  2020-05-01               0                0                     5
5  b@a.com  Bill  2020-05-02               0                5                     0

相关问题 更多 >

    热门问题