使用.drop和Pandas将单个行拖放一个值

问题描述 投票:0回答:1

具有发送电子邮件的代码,有些行与发送电子邮件的人的名字相同,但是每行都有唯一的值。我正在尝试发送一封包含所有值的电子邮件,其中在name列中有重复的单元格具有相同的名称,但是这些行中的所有唯一值。

因此,我尝试通过拉出具有重复行的行来制作重复数据帧,以使if else语句更容易。因为当我发送电子邮件时,它多次在不同的电子邮件中发送相同的值,所以我尝试这样做,因此每次发送电子邮件时,都会丢弃所使用的值所在的行。

在代码的末尾,而不是从单个值中删除一行,而是为每个行删除一个值。

import smtplib
import pandas as pd
import ssl
import xlrd

your_name = "Name Here"
your_email = "Email you using here"
your_password = "password to email here"
cc = input("Email of person you want to cc in email: ")

# for gmail change smtp-mail.outlook.com to smtp.gmail.com, 465
server = smtplib.SMTP_SSL('smtp-mail.outlook.com', 587)
server.ehlo()
server.login(your_email, your_password)

data = [['bob', '[email protected]', 'howdy'], ['joe', 
'[email protected]', 'hi'], ['bill', '[email protected]', 'hey'], 
['bob', '[email protected]', 'hola'],['bob', '[email protected]',    
'hello'], ['josh', '[email protected]', 'yo'], ['austin', 
'[email protected]', 'cya']

df = pd.DataFrame(data, columns = ['Pending Action From', 'Email', 
'values'])



all_names = email_list['Pending Action From']
all_emails = email_list['Email']
all_values = email_list['values']

# Takes duplicate row based off same name
duplicateRowsDF = email_list[email_list.duplicated(['Pending Action From'])]
duplicateRowsDF.to_excel('DuplicateQARList.xlsx', index=False)
duplicateRowsDF = pd.read_excel('DuplicateQARList.xlsx')

# removes duplicate row based off same name and keeps first same name
email_list.drop_duplicates(subset='Pending Action From', keep="first", inplace=True)

# email_list.to_excel('TestQARList.xlsx')


duplicate_values = duplicateRowsDF['value']
duplicate_names = duplicateRowsDF['Pending Action From']

 # Create the email to send
def full_email():
    full_email = ("From: {0} <{1}>\n"
                  "To: {2} <{3}>\n"
                  "Cc: {4}\n"
                  "Subject: {5}\n\n"
                  "{6}"
                  .format(your_name, your_email, name, email, cc, 
subject, message))
# Create the email to send

# In the email field, you can add multiple other emails if you want
# all of them to receive the same text
try:
    server.sendmail(your_email, [email], full_email)
    print('Email to {} successfully sent!\n\n'.format(email))
except Exception as e:
    print('Email to {} could not be sent :( because {}\n\n'.format(email, str(e)))


for idx in range(len(email_list)):

    email = all_emails[idx]
    name = all_names[idx]
    value = all_values[idx]

    for i in range(len(duplicateRowsDF)):

        duplicate_value = duplicate_values[i]
        duplicate_name = duplicate_names[i]

        if name == duplicate_name and value != duplicate_value and duplicate_names[i] == duplicate_names[i]:



            message = value, duplicate_values[i]

            full_email()
            email_list = email_list.drop(value)



# Close the smtp server
server.close()

Example of data frames made, it takes duplicates with the same name in the pending action from row and puts those rows in another data frame.

It only took two, but its supposed to take the data from all name values that are duplicate. It sends one email same their are duplicates of the same person, but it takes all the separate values for that one person.

python pandas dataframe email xlrd
1个回答
0
投票

在这种情况下,您似乎希望为每个行创建一个所有行值的列表,因此,与其一一对应地处理它们并放下它们,不如将它们汇总即可使用pop来删除它们。 ,或仅对分组的数据框进行操作

df.groupby(['Pending Action From','Email'], as_index=False).agg(list)

    Pending Action From Email   values
0   austin  [email protected] [cya]
1   bill    [email protected] [hey]
2   bob [email protected] [howdy, hola, hello]
3   joe [email protected] [hi]
4   josh    [email protected] [yo]
© www.soinside.com 2019 - 2024. All rights reserved.