Python package bar-chart-race 0.1.0: can't change separator thousands to dot

Viewed 128

While using pandas with the python package: bar-chart-race 0.1.0, I can't seem to change the thousands seperator to German/Dutch style,

namely: 1.000,23 instead of 1,000.23

I tried changing the locale

import locale

locale.setlocale(locale.LC_ALL, 'deu_DEU')

'German_Germany.1252'

"{0:n}".format(x)

It gives this feedback:

TypeError: unsupported format string passed to DataFrame.format

I tried changing the style

df.head().style.format("{0:n}")

This works partly, because I see the df values correctly displayed in the first rows of output, but it won't implement the changes in the bart chart race graphical representation of the data...

Any help would be greatly appreciated, please be warned, this is my very first python project :) I'm an absolute newbie.

1 Answers

When you install barchartrace via pip or conda, this currently installs an old version which doesn't allow you to change the text formatting locale.

So, uninstall the version you currently have and install the latest version of bar chart race as follows in your command line. You do need to have git installed for this:

pip uninstall bar_chart_race
pip install git+https://github.com/dexplo/bar_chart_race

And then you set your locale so that you get comma separators for decimals.
Working code example in Jupyter notebooks:

import bar_chart_race as bcr

import locale
locale.setlocale(locale.LC_NUMERIC, "de_DE")

df = bcr.load_dataset('covid19_tutorial')
bcr.bar_chart_race(
    df=df, 
    filename=None, 
    bar_texttemplate='{x:n}', 
    tick_template='{x:n}',
)
Related