Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为每个组按组提取数据_Python_Pandas_Pandas Groupby - Fatal编程技术网

Python 为每个组按组提取数据

Python 为每个组按组提取数据,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我有一张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

我有一张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
Bill               Regular Push ups     Easy Push ups   Difficult Pushups
      2020-05-01          10               0                    0
      2020-05-02          0                5                    0

发送电子邮件至:b@a.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
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天俯卧撑挑战赛,我们将此数据输入谷歌表单,这就像从谷歌表单发送给他们的确认电子邮件:)

使用:

在OP的评论之后: 您可以获取
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
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
你可以得到你的所有记录b@a.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
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
用于:

在OP的评论之后: 您可以获取
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
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
你可以得到你的所有记录b@a.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
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

下面是我如何解决这个问题的,如果你有任何优化的想法,请不要犹豫发表评论。非常感谢。感谢@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)

下面是我如何解决这个问题的,如果你有任何优化的想法,请不要犹豫发表评论。非常感谢。感谢@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)

我想获取电子邮件地址的记录a@b.com用它做点什么,然后把b#a.com的记录拿出来,用它做不同的事情。我说得通吗?我想访问要操纵的记录集(发送电子邮件至)@N2201请检查我更新答案的第二部分。我已经根据您的要求进行了更新。我添加了一个关于如何在您的帮助下解决此问题的答案。如果您能看一看,我将不胜感激。非常感谢。我想获取电子邮件地址的记录a@b.com用它做点什么,然后把b#a.com的记录拿出来,用它做不同的事情。我说得通吗?我想访问要操纵的记录集(发送电子邮件至)@N2201请检查我更新答案的第二部分。我已经根据您的要求进行了更新。我添加了一个关于如何在您的帮助下解决此问题的答案。如果您能看一看,我将不胜感激。非常感谢。