Formatting thousand separator for integers in a pandas dataframe

前端 未结 3 2326
自闭症患者
自闭症患者 2021-02-15 16:16

I\'m trying to use \'{:,}\'.format(number) like the example below to format a number in a pandas dataframe:

# This works for floats and integers
pri         


        
3条回答
  •  醉梦人生
    2021-02-15 16:53

    You can always cast your table to float64 and then use float_format as you like, especially if you are constructing a small table for viewing purposes. Instead of dealing with ints and floats separately this gives a quick solution.

    df.astype('float64',errors='ignore').to_html(float_format=lambda x: format(x,',.2f'))

    errors='ignore' is there to prevent raising an exception when a column can not be converted to floats, like strings.

提交回复
热议问题