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

前端 未结 7 894
予麋鹿
予麋鹿 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 12:00

    Until someone implements this in pandas, you can use the tabulate package:

    import pandas as pd
    from tabulate import tabulate
    
    def to_fwf(df, fname):
        content = tabulate(df.values.tolist(), list(df.columns), tablefmt="plain")
        open(fname, "w").write(content)
    
    pd.DataFrame.to_fwf = to_fwf
    
    0 讨论(0)
提交回复
热议问题