0

I have the following column containing a location (street name, x and y coordinates):

Location
"1139 57 STREET New York (40.632653207600001, -74.000244990799999)"

What I want to do is split it up into three columns: 'Address', 'Longitude' and 'Latitude'. Similar to this:

Location                   Latitude              Longitude
"1139 57 STREET New York   40.632653207600001    -74.000244990799999"

How would I go about doing this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Khaine775
  • 2,715
  • 8
  • 22
  • 51

2 Answers2

1

using str.extract

df.Location.str.extract(
    '^(?P<Location>.*)\s*\((?P<Latitude>[^,]*),\s*(?P<Longitude>\S*)\).*$',
    expand=True
)

                   Location            Latitude            Longitude
0  1139 57 STREET New York   40.632653207600001  -74.000244990799999
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0

Another idea that doesn't use regex, assuming your original data is consistently formatted:

def split_location(row):

    Location = row[:row.find('(')-1]
    Latitude = row[row.find('(')+1 : r.find(',')]
    Longitude = row[row.find(',')+2 :-1]

    return {'Location' : Location,
           'Latitude' : Latitude,
           'Longitude' : Longitude}

# original_df is a 1 column dataframe of Location (string) that you want to split
split_df = original_df[Location].apply(lambda x: split_location(x))
split_df = pd.DataFrame(list(split_df))
ilanman
  • 818
  • 7
  • 20