Saving a Pandas dataframe in fixed format with different column widths

后端 未结 1 1396
一生所求
一生所求 2021-01-27 06:31

I have a pandas dataframe (df) that looks like this:

   A   B  C
0  1  10  1234
1  2  20  0

I want to save this dataframe in a fixed format. Th

相关标签:
1条回答
  • 2021-01-27 07:24
    df['B'] = df['B'].apply(lambda t: (' '*(4-len(str(t)))+str(t)))
    df['C'] = df['C'].apply(lambda t: (' '*(5-len(str(t)))+str(t)))
    df.to_csv('path_to_file.csv', index=False)
    
    0 讨论(0)
提交回复
热议问题