Python 3.x 将列表中的所有字符串转换为浮点。适用于单个列表,但不适用于数据帧

Python 3.x 将列表中的所有字符串转换为浮点。适用于单个列表,但不适用于数据帧,python-3.x,string,pandas,floating-point,valueerror,Python 3.x,String,Pandas,Floating Point,Valueerror,我有一个数据帧df_tweets,带有地理定位功能。地理位置作为列表的字符串表示形式存储在变量geo_loc中。看起来是这样的: # Geocode values are stored as objects/strings df_tweets.geo_code[0] #Output: '[-4.241751 55.858303]' 我测试了将一行geo_code转换为一列经纬度浮动: # Converting string representation of list to list usi

我有一个数据帧
df_tweets
,带有地理定位功能。地理位置作为列表的字符串表示形式存储在变量
geo_loc
中。看起来是这样的:

# Geocode values are stored as objects/strings
df_tweets.geo_code[0]

#Output:
'[-4.241751 55.858303]'
我测试了将一行
geo_code
转换为一列经纬度浮动:

# Converting string representation of list to list using strip and split 
# Can't use json.loads() or ast.literal_eval() because there's no comma delimiter

#--- Test with one tweet ----#

ini_list = df_tweets.geo_code[0]

# Converting string to list, but it will convert
# the lon and lat values to strings
# i.e. ['-4.241751', '55.858303']

results = ini_list.strip('][').split(' ') 

# So, we must convert string lon and lat to floats
results = list(map(float, results))

# printing final result and its type 
print ("final list", results) 
print (type(result))
这给了我:

# Output:
final list [-4.241751, 55.858303]
<class 'list'>
当我跑步时:

df_tweets['geocode'] = df_tweets['geo_code'].apply(str_to_float_list)
当遇到减号时,它会给我一个
ValueError
。我不明白为什么?!我错过了什么

以下是全部错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-94-c1035312dc12> in <module>()
     20 
     21 
---> 22 df_tweets['geocode'] = df_tweets['geo_code'].apply(str_to_float_list)

1 frames
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-94-c1035312dc12> in str_to_float_list(list_as_str)
     15 
     16   # Convert strings inside list to float
---> 17   float_list = list(map(float, str_list[0]))
     18 
     19   return float_list

ValueError: could not convert string to float: '-'
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
20
21
--->22 df_tweets['geocode']=df_tweets['geo_code']。应用(str_to_float_列表)
1帧
pandas/_libs/lib.pyx在pandas中。_libs.lib.map_infere()
在str-to-float-list(list-as-str)中
15
16#将列表中的字符串转换为浮点
--->17浮动列表=列表(映射(浮动,str\u列表[0]))
18
19返回浮点数列表
ValueError:无法将字符串转换为浮点:'-'
在第17行

float_list = list(map(float, str_list[0]))
您不需要引用索引。将列表传递给整个列表,如下所示

float_list = list(map(float, str_list))

原因是str_list[0]是一个字符串对象,因此它试图将其视为一个列表,并以迭代方式转换每个值,首先将“-”转换为浮点,然后将“4”转换为其他字符。

如果数字中有像“-”这样的特殊字符,请尝试,除非看到哪一行返回错误code@YOandBEN_W对经度的负号。但在单个列表上运行代码时,这并不是问题。所以我不知道为什么,为什么申请整个专栏,不让我。这完全有道理。解决了
ValueError:无法将字符串转换为float:'-'
但现在引发了
ValueError:无法将字符串转换为float:
。不知道这是否与原始字符串的空间有关。但是
str_list=list_as_str.strip('][')。split('')
返回由逗号分隔的两个字符串组成的列表,所以不应该是这样。我将用您的答案和新错误更新问题。因此我尝试尽可能复制您的代码,并传递了“结果”我得到了一个不同的错误,但当我删除了方法代码中的行“str_list=list_as_str.strip('][').split('')”时,它立即起作用了。您是像变量“results”一样将列表传递给该方法,还是将预处理的字符串?list(map(float,results))传递给它我相信,应该为您提供与float相同的列表,而不需要帮助器方法。除非我误解了传递的是什么hanks。您的评论让我检查了传递的是什么。我做了以下操作:```lon_lat=[].\\\对于df_tweets['geo_code']中的行的变量中的每一行:``将字符串转换为list lon_lat=row.strip('].[')).split(“”)print(type(lon_lat))#将列表中的字符串转换为float lon_lat=list(map(float,lon_lat))``得到三个
,然后是值错误。问题是
df_tweets['geocode'][2]
。它之间有多个空格:
[-3.1798 51.497002]
。我可以对1、2和3个空格提出一个例外,看看它是如何进行的。嗯,是的。有很多解决方案。假设它永远不会超过3是一个答案。你可以使用正则表达式(不是我最喜欢的)。第三个是预处理,这是我个人会做的,迭代所有值并使它们一致。是的,肯定不是唯一的一个。将必须进行预处理。感谢帮助。将标记您的答案为解决方案,因为它确实解决了我原来的问题。
float_list = list(map(float, str_list))