How to replace a comma for an integer df point without having to do column for column - Python

Asked

Viewed 126 times

-2

I have the following code:

df10['Total_Forecast'] = df10['Total_Forecast'].apply(lambda x: float(x.replace(".","").replace(",",".")))

It’s working, but I need to apply replace column by column of df10. I have tried to take the name out of the column, but the code does not work to sweep all columns of df10 that has comma, turn point. There’s some way to do it for the whole county?

1 answer

2


just apply the rule to every dataframe

Creating Dataframe Test

>>> import pandas as pd

>>> df = pd.DataFrame({"A": ["Teste com, virgula", "Outro, teste"], "B": ["Nada aqui", "Mas, aqui tem"], "C": [1, 2]})

The dataframe

>>> df
                    A              B    C
0  Teste com, virgula      Nada aqui  1.0
1        Outro, teste  Mas, aqui tem  2.2

Converting

df1 = df.replace({',': '.'}, regex=True)

The Result

>>> df1
                    A              B    C
0  Teste com. virgula      Nada aqui  1.0
1        Outro. teste  Mas. aqui tem  2.2
  • But in this case, if I have columns that have no commas, but are int columns, returned Nan. Why?

  • 1

    Updated according to your comment.

  • It worked! Thank you!!! D

Browser other questions tagged

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