如何根据熊猫的条件将一列的值复制到另一列?

时间:2020-02-24 03:13:14

标签: python python-3.x pandas

如下所示,我的代码将票证发送到第一列中显示的电子邮件中。因此,如果有7张票证和2封电子邮件,该程序将通过电子邮件进行迭代并发送所有票证。我的问题是,所有电子邮件都发送完后,如何将电子邮件ID分配给所有者列。

示例:我确定所有票证都将发送到“ 1”列中的电子邮件ID,如何将“ 1”中的电子邮件ID分配给“所有者”列中的所有7行。

我在文件“ combo.csv”中输入以下内容

0            1                 2                 3     owner
0   0   sunith@email.com    116675  Random Message 1    NaN
1   1   sunith@email.com    116674  Random Message 2    NaN
2   2   NaN                 116675  Random Message 3    NaN
3   3   NaN                 116676  Random Message 4    NaN
4   4   NaN                 116677  Random Message 5    NaN
5   5   NaN                 116678  Random Message 6    NaN
6   6   NaN                 116679  Random Message 7    NaN

请帮助

e = pd.read_csv('combo.csv')
f = e.fillna("")
print(f)
emails = f['1'].values
email_index = 0
count_of_emails = len(emails)
print("count of emails" + str(count_of_emails))
ticket_number = list(f['2'].values)
desc_value = f['3'].values
for ticket_index in range(len(ticket_number)):
    if not emails[email_index]:
        continue
    if email_index != count_of_emails:
        print("resetting email Index")
        email_index = 0
    outlook = win32com.client.Dispatch("Outlook.Application")
    if not ticket_number[ticket_index]:
        message = f'Random Message'
    else:
        message = f'Hello the ticket number {int(ticket_number[ticket_index])} with the Description {desc_value[ticket_index]} has been assigned to ' \
                  f'you. '
        adding_owner = pd.read_csv('combo.csv')
        adding_owner['owner'] = emails
        owner_added = adding_owner.copy()
        # del owner_added['4']
        owner_added = owner_added.loc[:, ~owner_added.columns.str.contains('^Unnamed')]
        owner_added.to_csv('combo.csv')

    mail = outlook.CreateItem(0)
    mail.To = emails[email_index]
    mail.Subject = 'This is a test'
    mail.HTMLBody = message
    mail.Send()
    print("Sent email to" + emails[email_index])
    email_index = email_index + 1

如果所有电子邮件都已发送,则输出应类似于

Output:

   0            1                  2                 3     owner
    0   0   sunith@email.com    116675  Random Message 1    sunith@email.com
    1   1   sunith@email.com    116674  Random Message 2    sunith@email.com
    2   2   NaN                 116675  Random Message 3    sunith@email.com
    3   3   NaN                 116676  Random Message 4    sunith@email.com
    4   4   NaN                 116677  Random Message 5    sunith@email.com
    5   5   NaN                 116678  Random Message 6    sunith@email.com
    6   6   NaN                 116679  Random Message 7    sunith@email.com

1 个答案:

答案 0 :(得分:0)

假定每个唯一电子邮件将表细分为.csv,最简单的解决方案是:

In [57]: test.iloc[:, 0] = df.iloc[:, 3].ffill()

In [58]: df
Out[58]:
                  0       1               2                 3
0  sunith@email.com  116675               1  sunith@email.com
1  sunith@email.com  116674               2  sunith@email.com
2               NaN  116675               3  sunith@email.com
3               NaN  116676               4  sunith@email.com
4               NaN  116677               5  sunith@email.com
5               NaN  116678               6  sunith@email.com
6               NaN  116679               7  sunith@email.com