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

后端 未结 6 1694
深忆病人
深忆病人 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:00

    The to_string approach suggested by @mattexx looks better to me, since it doesn't modify the dataframe.

    It also generalizes well when using jupyter notebooks to get pretty HTML output, via the to_html method. Here we set a new default precision of 4, and override it to get 5 digits for a particular column wider:

    from IPython.display import HTML
    from IPython.display import display
    
    pd.set_option('precision', 4)
    
    display(HTML(df.to_html(formatters={'wider': '{:,.5f}'.format})))
    

提交回复
热议问题