0

I need all numbers in my pandas dataframes converted to german number notation. It means there should be thousands decimal dot and a comma for fractions.

example.:

52.634,99

I can do it easily the other way around, first comma, then dot:

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

Why is it not possible to use it this way?

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

error:

ValueError: Format specifier missing precision
stanvooz
  • 522
  • 3
  • 19
  • A hacky way would be to use the method that works, then replace commas by dots and viceversa using `string.replace()` –  Nov 29 '21 at 14:32
  • Does Pandas use the current locale to format values? – chepner Nov 29 '21 at 14:33
  • 1
    Your attempt doesn't work because the order of things in the field format is strictly defined; it's not just a set of characters. – chepner Nov 29 '21 at 14:33
  • See also https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python . – Karl Knechtel Nov 29 '21 at 14:42

1 Answers1

1

converted to german number notation

This is task for locale built-in module, you can use it following way:

import locale
import pandas as pd
locale.setlocale(locale.LC_NUMERIC, 'de_DE')
pd.options.display.float_format = lambda x:locale.format_string('%.2f',x,True)
df = pd.DataFrame({'x':[5663499e-2]})
print(df)

output

          x
0 56.634,99

Explanation: use german (de_DE) numeric convention, '%.2f' is 2 digits after ,, x is value itself, True is use thousands sep. For futher discussion see locale.format_string in docs.

Daweo
  • 31,313
  • 3
  • 12
  • 25