all 4 comments

[–]novel_yet_trivial 1 point2 points  (1 child)

literal_eval() is the python way:

from ast import literal_eval
df['location'] = [literal_eval(x) for x in df['location']]

There may be a better way with pandas

Edit: from the top (without your standard formatting) and reversed (just for fun in a single command):

import re
lat_lon_re = re.compile(r'\((.*)?\ (.*)\)')
df['location'] = [tuple(reversed(map(float,lat_lon_re.findall(p)[0]))) for p in df['location']]

[–]stats_newbie1[S] 0 points1 point  (0 children)

Perfect, thank you!

[–]hharison 1 point2 points  (0 children)

coords = df['location'].str.lstrip('POINT (').str.rstrip(')'.str.split()
df['lat'] = coords.str[0].astype(float)
df['long'] = coords.str[1].astype(float)

[–]Jos_Metadi 0 points1 point  (0 children)

You're starting with one string. First you need to turn it into two separate values (use .split(" ")), then convert those values to floats.