【问题标题】:Can't convert object to float无法将对象转换为浮动
【发布时间】:2018-10-02 16:48:05
【问题描述】:

我需要将一个对象转换为浮点数以稍后组合一些数字,但似乎无法做到。我正在处理我机器上的文件,但如果有人想复制,数据就在网络上。

original csv

尝试转换,因为它来自 csv

crime = pd.read_csv("C://college_data/nrippner-opportunity-project-use-case/Crime_2015.csv", dtype={'PropertyCrime':float})
print(crime.head())
crime.dtypes

似乎是“安全规则”的问题

我也试过了

crime['PropertyCrime'] = crime.PropertyCrime.astype(float)

它只是说不能将对象转换为浮点数

有什么想法吗?

根据 cmets 的要求:

crime = pd.read_csv("C://college_data/nrippner-opportunity-project-use-case/Crime_2015.csv")

print(crime.PropertyCrime.head())
crime.dtypes

抱歉,如果有比屏幕截图更好的方法从 jupyter notebook 发布,我深表歉意

【问题讨论】:

    标签: python pandas dataframe type-conversion data-cleaning


    【解决方案1】:

    由于,,数字无法正确推断。

    样本数据

                        MSA ViolentCrime  Murder  Rape  Robbery  AggravatedAssault PropertyCrime Burglary    Theft  MotorVehicleTheft State         City
         Abilene, TX M.S.A.        412.5     5.3  56.0     78.4              272.8       3,609.0    852.0  2,493.6              263.4    TX      Abilene
           Akron, OH M.S.A.        238.4     5.1  38.2     75.2              119.8       2,552.4    575.3  1,853.0              124.1    OH        Akron
          Albany, GA M.S.A.        667.9     7.8  30.4    157.9              471.8       3,894.1  1,099.6  2,652.8              141.7    GA       Albany
          Albany, OR M.S.A.        114.3     2.5  28.2     20.7               63.0       3,208.4    484.6  2,476.1              247.7    OR       Albany
     Albuquerque, NM M.S.A.        792.6     6.1  63.8    206.7              516.0       4,607.8    883.4  3,047.6              676.9    NM  Albuquerque
    
    import pandas as pd
    
    df = pd.read_csv('https://query.data.world/s/27rl5szyyfje5zv5dg2us5c5vqlcfz', thousands=',')
    
    • 现在可以正确推断浮点类型。
                        MSA  ViolentCrime  Murder  Rape  Robbery  AggravatedAssault  PropertyCrime  Burglary   Theft  MotorVehicleTheft State         City
         Abilene, TX M.S.A.         412.5     5.3  56.0     78.4              272.8         3609.0     852.0  2493.6              263.4    TX      Abilene
           Akron, OH M.S.A.         238.4     5.1  38.2     75.2              119.8         2552.4     575.3  1853.0              124.1    OH        Akron
          Albany, GA M.S.A.         667.9     7.8  30.4    157.9              471.8         3894.1    1099.6  2652.8              141.7    GA       Albany
          Albany, OR M.S.A.         114.3     2.5  28.2     20.7               63.0         3208.4     484.6  2476.1              247.7    OR       Albany
     Albuquerque, NM M.S.A.         792.6     6.1  63.8    206.7              516.0         4607.8     883.4  3047.6              676.9    NM  Albuquerque
    
    df.info()
    
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 378 entries, 0 to 377
    Data columns (total 12 columns):
    MSA                  378 non-null object
    ViolentCrime         377 non-null float64
    Murder               378 non-null float64
    Rape                 378 non-null float64
    Robbery              378 non-null float64
    AggravatedAssault    377 non-null float64
    PropertyCrime        372 non-null float64
    Burglary             374 non-null float64
    Theft                375 non-null float64
    MotorVehicleTheft    378 non-null float64
    State                378 non-null object
    City                 373 non-null object
    dtypes: float64(9), object(3)
    

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 2012-07-02
      • 1970-01-01
      • 2021-08-13
      • 2021-12-11
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多