无法将对象转换为浮点数

Can't convert object to float

我需要将对象转换为浮点数以便稍后组合一些数字,但似乎做不到。我正在使用我机器上的文件工作,但如果有人想复制的话,数据在网络上。

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

似乎是 'safe rule'

的问题

我也试过了

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

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

有什么想法吗?

根据评论要求:

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

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

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

由于 ,.

,无法正确推断出数字

示例数据

                    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)