Search code examples
python-3.xstringpandasfloating-pointvalueerror

Convert all strings in a list to float. Works on single list but not when applied to dataframe


I've got a dataframe df_tweets with geolocation. The geolocation is stored in a variable geo_loc as a string representation of a list. It looks like this:

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

#Output:
'[-4.241751 55.858303]'

I tested converting one row of geo_code to a list of longitude-latitude as floats:

# 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))

This gives me:

# Output:
final list [-4.241751, 55.858303]
<class 'list'>

Success! Except no. I wrote it as a helper function:

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

And when I run:

df_tweets['geocode'] = df_tweets['geo_code'].apply(str_to_float_list)

it gives me a ValueError when it encounters the minus - sign. I can't figure out why?! What am I missing?

Here's the full error:

---------------------------------------------------------------------------
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: '-'

Solution

  • On your line 17,

    float_list = list(map(float, str_list[0]))
    

    You do not need to reference the index. Pass the list the entire list, like this.

    float_list = list(map(float, str_list))
    

    The reason for this is str_list[0] is a string object, so it is trying to treat it like a list, and converting each value iteratively, starting with converting "-" to a float, then it would convert "4," etc.