swap decimal separator in python

Asked

Viewed 438 times

-1

How do I change the decimal point separator to comma and then generate a graph? I used the following command, no error, but the chart continues with point as decimal separator

   import locale
   loc = locale.getlocale()
   locale.setlocale(locale.LC_NUMERIC, "pt_BR")
  • Are you using a data file? Are you generating this data at random? Show your data output. Edit your question so that it is reproducible by everyone, so we can help you more efficiently. Hug!

  • 1

    Thanks for the feedback. The code is very simple, I want to plot the graph of a time series, but with decimal separator as comma since the article is in Portuguese. I imported the pandas, matplotlib and locale libraries. Then I read the database in CSV (pd.read_csv('data.csv', parse_dates=[0], index_col=0, Squeeze=True) and have plotted the graph. But it keeps generating the dot graph.

1 answer

0

If you need a string, you can simply replace the point with a comma with the replace(), but I don’t think it’s very viable if you have to do it over and over again.

numero_com_ponto = 123.45
numero_com_virgula = str(numero_com_ponto).replace('.', ',')
  • Thanks for the feedback. Would you like to tell me if you can do this replace process when importing the database*? Another question would be whether transforming the type of a float variable into a string would not compromise the graph generation? *data = pd.read_csv('data.csv', parse_dates=[0], index_col=0, Squeeze=True).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.