0
df.info()
>>14  Receita_Praca_de_Pedagio  1524 non-null   category
type(df.Receita_Praca_de_Pedagio[2])
>>str

I converted the Object type to Category and all in this column are Strings.

The data in this column are measured in Brazilian currency R $ (real). I need to transform the items in this column to type Int to be able to create models and do anything else

But I cannot change to numeric type. I tried two things, to no avail

pd.to_numeric(df.Receita_Praca_de_Pedagio, downcast='float')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()

ValueError: Unable to parse string "15552503,290768042"

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-83-6f06b3abb22d> in <module>
----> 1 pd.to_numeric(df.Receita_Praca_de_Pedagio, downcast='float')

~\anaconda3\lib\site-packages\pandas\core\tools\numeric.py in to_numeric(arg, errors, downcast)
    147         coerce_numeric = errors not in ("ignore", "raise")
    148         try:
--> 149             values = lib.maybe_convert_numeric(
    150                 values, set(), coerce_numeric=coerce_numeric
    151             )

pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()

ValueError: Unable to parse string "15552503,290768042" at position 0

The other attempt to change the String to Int was:

df.Receita_Praca_de_Pedagio = df.Receita_Praca_de_Pedagio.astype('uint64')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-88-144a36e6b86a> in <module>
----> 1 df.Receita_Praca_de_Pedagio = df.Receita_Praca_de_Pedagio.astype('uint64')

.
.
.

---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

ValueError: invalid literal for int() with base 10: '15552503,290768042'

Can anyone help me how to convert this data to Int?

LpCoutinho
  • 45
  • 4
  • `df = pd.DataFrame({'col' : ['123,4334']})` `df['col'] = df['col'].str.replace(',','')` You have a comma, which you need to replace. Then, you can convert. – David Erickson Sep 11 '20 at 04:45
  • Does this answer your question? [How to replace text in a column of a Pandas dataframe?](https://stackoverflow.com/questions/28986489/how-to-replace-text-in-a-column-of-a-pandas-dataframe) – David Erickson Sep 11 '20 at 04:56

0 Answers0