ValueError:无法解析字符串" notpresent"在0号位置

时间:2018-05-14 03:16:43

标签: python pandas

我正在编写一个简单的代码,但是出现此错误,请帮助解决此问题。

import math
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from sklearn import preprocessing

df= pd.read_html('https://github.com/authman/DAT210x/blob/master/Module4/Datasets/kidney_disease.csv')[0]

# drop id column

df.drop('id', axis = 1, inplace = True)

#Get rid of any row that has at least 1 NANs in it ## Drop the rows where any of the elements is nan

df.dropna(axis = 0, how = 'any', inplace = True)

# Use an indexer to select only the following columns:
# ['bgr','wc','rc']

df = df[['bgr', 'wc', 'rc']]
df.dtypes

bgr    object
wc     object
rc     object
dtype: object

df.head(5)
    bgr wc  rc
0   notpresent  32  6700
1   notpresent  29  12100
2   notpresent  32  4500
3   present 16  11000
4   notpresent  24  9200

# Now they are all floats

df.bgr = pd.to_numeric(df.bgr, downcast='float')
df.wc = pd.to_numeric(df.wc, downcast='float')
df.rc = pd.to_numeric(df.rc, downcast='float')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
pandas\src\inference.pyx in pandas.lib.maybe_convert_numeric (pandas\lib.c:55708)()

ValueError: Unable to parse string "notpresent"

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-246-24d33f210233> in <module>()
----> 1 df.bgr = pd.to_numeric(df.bgr, downcast='float')
      2 df.wc = pd.to_numeric(df.wc, downcast='float')
      3 df.rc = pd.to_numeric(df.rc, downcast='float')

C:\Users\hp\Anaconda3\lib\site-packages\pandas\tools\util.py in to_numeric(arg, errors, downcast)
    193             coerce_numeric = False if errors in ('ignore', 'raise') else True
    194             values = lib.maybe_convert_numeric(values, set(),
--> 195                                                coerce_numeric=coerce_numeric)
    196 
    197     except Exception:

pandas\src\inference.pyx in pandas.lib.maybe_convert_numeric (pandas\lib.c:56097)()

ValueError: Unable to parse string "notpresent" at position 0

0 个答案:

没有答案