Python Pandas, write DataFrame to fixed-width file (to_fwf?)

前端 未结 7 971
予麋鹿
予麋鹿 2020-11-30 11:25

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, a

7条回答
  •  半阙折子戏
    2020-11-30 11:57

    For custom format for each column you can set format for whole line. fmt param provides formatting for each line

    with open('output.dat') as ofile:
         fmt = '%.0f %02.0f %4.1f %3.0f %4.0f %4.1f %4.0f %4.1f %4.0f'
         np.savetxt(ofile, df.values, fmt=fmt)
    

提交回复
热议问题