【问题标题】:Using .drop with Pandas to drop a single row by a value使用 .drop 和 Pandas 将单行删除一个值
【发布时间】:2020-07-05 10:19:29
【问题描述】:

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

因此,为此我尝试通过提取具有重复行的行来制作重复的数据框,以使 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', 'testemail@gmail.com', 'howdy'], ['joe', 
'testemail@gmail.com', 'hi'], ['bill', 'testemail@gmail.com', 'hey'], 
['bob', 'testemail@gmail.com', 'hola'],['bob', 'testemail@gmail.com',    
'hello'], ['josh', 'testemail@gmail.com', 'yo'], ['austin', 
'testemail@gmail.com', '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.

【问题讨论】:

  • 欢迎堆栈溢出!您能否提供一个minimal reproducible example,包括您的输入数据框样本,以便我们更好地了解您的问题?似乎您可以使用 groupby,但很难说
  • 感谢您的反馈,我添加了一些图片让我知道是否有帮助。谢谢!
  • 感谢更新,但在问题的文本中包含示例数据很重要,而不是图片或链接。我们无法重现或测试数据图片。见How to make good pandas examples
  • 知道了,应该包括所有内容。我刚刚在文件中复制了数据框。谢谢!
  • 我仍然不清楚你的任务,但是使用df.groupby(['Pending Action From','Email']).agg(list) 将所有'values' 按名称/电子邮件分组,然后采取行动是否有用?那你就不用担心重复的名字了,你可以pop每一行并处理它来发送电子邮件...?

标签: python pandas dataframe email xlrd


【解决方案1】:

在这种情况下,您似乎想要为每个行列出所有行值,因此您可以聚合它们并使用 pop 来删除它们,而不是一个接一个地处理它们并删除它们每个,或者只对分组的数据框进行操作

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

    Pending Action From Email   values
0   austin  testemail@gmail.com [cya]
1   bill    testemail@gmail.com [hey]
2   bob testemail@gmail.com [howdy, hola, hello]
3   joe testemail@gmail.com [hi]
4   josh    testemail@gmail.com [yo]

【讨论】:

    猜你喜欢
    • 2022-08-20
    • 2016-10-30
    • 2017-10-24
    • 2023-01-20
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多