【问题标题】:how to convert an object to int in python如何在python中将对象转换为int
【发布时间】:2022-09-24 16:33:45
【问题描述】:

我正在尝试将对象转换为 int,但它给了我一个 ValueError 错误:int() 以 10 为基数的无效文字:\'323.0\'

alltrips.dtypes
tripduration         object
from_station_id      object
from_station_name    object
to_station_id        object
to_station_name      object
usertype             object
gender               object
birthyear             int32
dtype: object

display (alltrips.tripduration)
0           323.0
1           377.0
2         2,904.0
3           747.0
4           183.0
           ...   
642681      288.0
642682      514.0
642683      427.0
642684      200.0
642685    1,285.0
Name: tripduration, Length: 3040517, dtype: object

alltrips[\"tripduration\"] = alltrips[\"tripduration\"].astype(int)

    标签: python type-conversion


    【解决方案1】:

    尝试:

    alltrips["tripduration"] = alltrips["tripduration"].astype(str).astype(int)
    

    它将转换为 string 然后 int

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 2012-01-24
      • 2019-12-01
      • 2010-10-29
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      相关资源
      最近更新 更多