Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: write df to text file - indent df to right by 5 white spaces

I am writing a df to a text file like so:

file = open("pptx_comparision_log.txt", "w")
df= df.to_string()
file.write(comp_df)

This works fine but how can I indent my df so it sits 5 white spaces to the right.

so from this:

                    dim_pptx  qp_pptx
Absolute Radio        0.0739   0.0753
BBC Asian Network     0.0013   0.0013
BBC Radio 1           0.1441   0.1455
BBC Radio 1Xtra       0.0057   0.0058
BBC Radio 2           0.2336   0.2339

to:

                         dim_pptx  qp_pptx
     Absolute Radio        0.0739   0.0753
     BBC Asian Network     0.0013   0.0013
     BBC Radio 1           0.1441   0.1455
     BBC Radio 1Xtra       0.0057   0.0058
     BBC Radio 2           0.2336   0.2339

Is this possible?

Thanks.

like image 212
Boosted_d16 Avatar asked Oct 22 '15 14:10

Boosted_d16


1 Answers

You can just do it with string manipulation after the conversion by replacing df.to_string() with " "*5 + df.to_string().replace("\n", "\n ").

like image 162
Randy Avatar answered Nov 15 '22 01:11

Randy