0

I'm trying to create a pandas data frame using data that I'm collecting from the web.So every time there is a login ,the data should be stored in a data frame that i will save as an excel file. I have a code that creates the data frame and writes the data, but it does not append the data, so whenever there's a new entry it overwrites the existing information that was in the excel file.

Python Code:

count = 0
data = []
df = pd.DataFrame(columns=['Date','Country','Name','Age'])
df.loc[count, 'Date'] = datetime.datetime.now().strftime('%d %B %Y')
df.loc[count, 'Country'] = driver.find_element_by_name('country').get_attribute('value')
df.loc[count, 'Name'] = driver.find_element_by_name('customername').get_attribute('value')
df.loc[count, 'Age'] = driver.find_element_by_name('acli.age').get_attribute('value')
df.to_excel("C:\Web Data\MIS REPORT.xlsx")

Instead of the code overwriting the existing rows in the excel file it should append it.

1 Answer 1

1

You did not increment your count every time there is a new entry, hence it's always overwriting the first entry of the dataframe!

Also, with each entry, the following lines:

count = 0
data = []
df = pd.DataFrame(columns=['Date','Country','Name','Age'])

should not be executed again so as to not recreate the dataframe/reset the count back to 0.

Hope that helps! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.