Skip to content
Advertisement

display pandas dataframe with european formatting

I found answers on the question on how to load csv files with european formatting into pandas dataframes and display them in the US format (2,345.21). However how can I display floats saved in the US format in the european format (2.345,21) in pandas.

I tried to just change dot and commas here:

from

pd.options.display.float_format = '{:,.2f}'.format

to

pd.options.display.float_format = '{:.,2f}'.format

but this doesn’t work. Another possibility could be to change the type to string and then replace dots with commas, but isn’t there a more elegant way?


It seems it works with locale aware seperators. If your operating system has the locale Germany it is pretty easy:

import pandas as pd
import locale
locale.setlocale(locale.LC_ALL, '')
pd.options.display.float_format = '{:n}'.format
df = pd.DataFrame([2123.4567, 234.5678, 345.6789, 456.7890],
                  index=['foo','bar','baz','quux'],
                  columns=['cost'])
print(df)

results to:

         cost
foo  2.123,46
bar   234,568
baz   345,679
quux  456,789

However now I am dealing with the problem to format the precision and the format for high numbers. As the result of the following format command is somewhat surprising.

#precision
pd.options.display.float_format = '{:.2n}'.format
print(df)

        cost
foo  2,1e+03
bar  2,3e+02
baz  3,5e+02
quux 4,6e+02

#high numbers
pd.options.display.float_format = '{:n}'.format
df = pd.DataFrame([1222333, 234.5678, 345.6789, 456.7890],
                  index=['foo','bar','baz','quux'],
                  columns=['cost'])
print(df)

            cost
foo  1,22233e+06
bar      234,568
baz      345,679
quux     456,789

Advertisement

Answer

Before you begin:

conda install babel

Then try this:

from babel.numbers import decimal, format_decimal
format_decimal(22222345.22, format='#,##0.##;-#', locale='de')
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement