Output different precision by column with pandas.DataFrame.to_csv()?

后端 未结 6 1697
深忆病人
深忆病人 2020-12-13 02:51

Question

Is it possible to specify a float precision specifically for each column to be printed by the Python pandas package method pandas.DataFrame.t

6条回答
  •  遥遥无期
    2020-12-13 03:23

    This question is a bit old, but I'd like to contribute with a better answer, I think so:

    formats = {'lats': '{:10.5f}', 'lons': '{:.3E}', 'vals': '{:2.1f}'}
    
    for col, f in formats.items():
        df_data[col] = df_data[col].map(lambda x: f.format(x))
    

    I tried with the solution here, but it didn't work for me, I decided to experiment with previus solutions given here combined with that from the link above.

提交回复
热议问题