如何使用Pandas转换Int中的字符串?

2024-09-28 03:17:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我将对象类型转换为类别,此列中的所有内容都是字符串

本栏数据以巴西货币R$(雷亚尔)计量。 我需要将本专栏中的项目转换为Int类型,以便能够创建模型和执行其他任何操作

但是 我无法更改为数字类型。我试了两件事,都没用

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

将字符串更改为Int的另一次尝试是:

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'

有人能帮我把这些数据转换成Int吗


Tags: toinpandasdflibdelibsint

热门问题