I see that Pandas has read_fwf
, but does it have something like DataFrame.to_fwf
? I'm looking for support for field width, numerical precision, and string justification. It seems that DataFrame.to_csv
doesn't do this. numpy.savetxt
does, but I wouldn't want to do:
numpy.savetxt('myfile.txt', mydataframe.to_records(), fmt='some format')
That just seems wrong. Your ideas are much appreciated.
Until someone implements this in pandas, you can use the tabulate package:
For custom format for each column you can set format for whole line. fmt param provides formatting for each line
Python, Pandas : write content of DataFrame into text File
The question aboves answer helped me. It is not the best, but until
to_fwf
exists this will do the trick for me...or
I'm sure you found a workaround for this issue but for anyone else who is curious... If you write the DF into a list, you can write it out to a file by giving the 'format as a string'.format(list indices) eg:
Just make sure to match up each index with the correct format :)
Hope that helps!