【发布时间】:2020-05-09 22:46:53
【问题描述】:
我有一个带有地理位置的数据框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'>
成功!除了没有。我把它写成一个辅助函数:
def str_to_float_list(list_as_str):
'''
Function to convert a string representation
of a list into a list of floats
using strip and split, when you can't use json.loads()
or ast.literal_eval() because there's no comma delimiter
Parameter:
str_ = string representation of a list.
'''
# Convert string to list
str_list = list_as_str.strip('][').split(' ')
# Convert strings inside list to float
float_list = list(map(float, str_list[0]))
return float_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: '-'
【问题讨论】:
-
您的号码中有“-”之类的特殊字符?请尝试,除了查看哪一行返回错误代码
-
@YOandBEN_W 是的,经度的减号。但是在单个列表上运行代码时这不是问题。所以我不知道为什么,为什么申请整个专栏,不让我。
标签: python-3.x string pandas floating-point valueerror