0

I am using the below code in a for loop, the problem is whenever a new loop starts, a new csv is formed that means deleting the previous entries. I want that the entries get appended. I can do it using csv write but would be nice to know using pandas.

prediction.to_csv("tmp.csv" , sep='\t', encoding='utf-8', doublequote=False, index=False)

1 Answer 1

2

As with most file operations you can tell them which way you want to open the file with:

Doku: to_csv

Just use the append mode:

prediction.to_csv("tmp.csv" , sep='\t', encoding='utf-8', \
    doublequote=False, index=False, mode="a")

You might also want to include a

useHeader = True 

before your loop and modify your command to:

prediction.to_csv("tmp.csv" , sep='\t', encoding='utf-8', \
    doublequote=False, index=False, mode="a", header = useHeader)
useHeader = False

so you only get one header on top of the file.

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

1 Comment

Thanks a lot for the help. :)

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.