0

I'm wondering if there is a method to change the string formatting in pandas.DataFrame.to_csv.

In particular, I'd like to set each column header to something like %20s so that they are all evenly spaced out in the file. I read this stackexchange but I was hoping to do it without writing the header beforehand.

Thanks in advance.

1 Answer 1

0

I don't think to_csv has a built in header formatting function, but you can always loop over the headers and fill them to the full length before printing. Something like

df.columns = [column_header.rjust(20, ' ') for column_header in df.columns]
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.