Python ValueError中未识别的变量类型:无法将字符串转换为浮点:

Python ValueError中未识别的变量类型:无法将字符串转换为浮点:,python,Python,我有这个专栏 In [] : parsed_data_prodiTI_smt8['IP'].unique() Out [] : array(['3.69', '4', '3.73', '3.24', '3.56', '3.3', '3.31', '3.74', '3.37', '3.19', '3.43', '3.5', '3.64', '3.46', '3.79', '3.14', '3.81', '3.21', '3.96', '3.7', '3.6', '3

我有这个专栏

In  [] : parsed_data_prodiTI_smt8['IP'].unique()
Out [] : 
array(['3.69', '4', '3.73', '3.24', '3.56', '3.3', '3.31', '3.74', '3.37',
       '3.19', '3.43', '3.5', '3.64', '3.46', '3.79', '3.14', '3.81',
       '3.21', '3.96', '3.7', '3.6', '3.55', '3.25', '3.07', '3.51',
       '3.67', '2.98', '3', '3.18', '3.53', '3.48', '2.93', '2.82',
       '3.39', '3.36', '1.62', '3.12', '3.27', '3.82', '3.89', '3.47',
       '3.68', '3.95', '3.86', '3.54', '3.63', '3.88', '2.8', '3.9',
       '3.4', '', '3.23', '3.78', '3.45', '3.41', '3.85', '3.52', '3.35', #Notice there's '' in this row
       '3.32', '3.66', '3.38', '3.71', '3.75'], dtype=object)

# I don't know what is it, I thought it's null so I checked it out like this

In  [] : parsed_data_prodiTI_smt8['IP'].isnull().sum()
Out [] : 0 

In  [] : parsed_data_prodiTI_smt8[parsed_data_prodiTI_smt8['IP']=='']['IP'].isnull()
Out [] : False

# Resulting if I do this, it returns error
In  [] : parsed_data_prodiTI_smt8['IP'].astype('float')
Out [] : ValueError: could not convert string to float:

我不知道它是什么,显然它不是空的,也不是空白,因为当我像
==''
一样查找它时,它不返回任何行。因为我不知道那是什么,我不知道如何处理这个错误。有人能帮忙吗?我需要将该列更改为float,而未识别的
'
正在阻塞我的路径

您必须检查是否忽略了'。试试这个

condition = parsed_data_prodiTI_smt8['IP']== ''
array = parsed_data_prodiTI_smt8['IP']
np.where(condition,array,array.astyp('float')

列中有一个空字符串
'
(它不同于您正在检查的单个空格字符串
'
),无法转换为
浮点值。你会在
float(“”)
中得到同样的错误。好吧,在查找其他案例之后,他们显然使用了
.str.extract('(\d)).astype('float')
,但它会将我需要捕获的
后面的两位数切掉,例如
3.41
。有没有办法做到这一点?这能回答你的问题吗?