Add Loop Values to each row in DF

Viewed 17

Hi so I was hoping to build out a dataframe to manipulate some information, however it is challenging with the win32com package. I tired what I have below but I only get 1 value for message. I was wondering how to turn that into every instance of message that occurs in the loop.

import win32com.client as client
import csv
import pandas as pd

outlook = client.Dispatch('Outlook.Application')

namespace = outlook.GetNameSpace('MAPI')

account = namespace.Folders['rim5532@psu.edu']

inbox = account.Folders['Inbox']

EM = [message for message in inbox.Items]

for message in EM:
    m1 = message
    df = pd.DataFrame({"Message":[m1]})

print(df)
1 Answers

The issue with your code is that df is overwritten in each step of the loop. Try the below version:

import win32com.client as client
import csv
import pandas as pd

outlook = client.Dispatch('Outlook.Application')
namespace = outlook.GetNameSpace('MAPI')
account = namespace.Folders['rim5532@psu.edu']
inbox = account.Folders['Inbox']

EM = [message for message in inbox.Items]

data = []

for message in EM:
    m1 = message
    data.append({"Message":[m1]})

df = pd.DataFrame(data)
print(df)
Related