0

I am trying to write Dataframe to csv :

for i in range 10:
    consolidated_df.to_csv(output_file_path + '/' +   dest_csv,mode='a',index=False)

This is creating adding headers as new rows for every iteration

col1 col2 col3
a     b     c
col1 col2  col3
d    e      f
col  col2   col3
g    h      i

if i use header=none in df.to_csv , then the csv doesnt have any headers at all

all i need is this

col1 col2 col3
a     b     c
d    e      f
g    h      i

1 Answer 1

1

One approach is to check if index is 0 then add header else set header to None

Ex:

for i in range(10):
    if i == 0:
        consolidated_df.to_csv(output_file_path + '/' +  dest_csv,mode='a',index=False)
    else:
        consolidated_df.to_csv(output_file_path + '/' +  dest_csv,mode='a',index=False, header=None)
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.