So I am parsing data contained in many files, looping through them and storing certain elements in a list, and appending each resulting list to a dataframe with Pandas using Python.
It works, except I can't figure out how to keep the header row while appending. It either disappears or is duplicated with each append.
The below code serves as an example of the latest code:
import pandas as pd
for i in range(1,4):
data = [{'name': 'Company'+str(i), 'city': 'New York'}]
stuff = []
for element in data:
stuff.append(element)
df = pd.DataFrame(columns=["name",
"city"])
for record in stuff:
df = df.append(record, ignore_index=True)
df.to_csv('test.csv', mode='a', header=False, index=False)
With this code the output (csv file) is:
Company1 New York
Company2 New York
Company3 New York
But I am looking for the output to be:
name city
Company1 New York
Company2 New York
Company3 New York
Thanks!