I'm trying to write a pandas dataframe to a CSV file with the associated headers in tact row by row. I've accomplished this using
new_df = pd.DataFrame(old_df).T
Which appears to produce a format (I think a dict?) where I can then write to a csv file line by line using the following
with open('new.csv', 'a') as f:
new_df.to_csv(f)
However in the CSV file I have two new columns both with incremental numbers (1,2,3..) for each row. The second column has the key 'Unnamed: 0' while the first column has no key (i.e. key = '').
I would prefer to not have these headers, I can delete the second on using
del new_df['Unnamed: 0']
but I can't do this for the second as it has no key (del new_df[''] does not work).
Would anyone know either how to delete the first column or a better way to write a dataframe to a csv file row by row..
df.iloc[:, 1:].to_csv(...)