Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Lat Lon coordinates to separate columns (python/dataframe)

I'm sure this is a simple thing to do but I am new to Python and cannot work it out!

I have a data frame with one column containing coordinates and I am wanting to remove the brackets and add the Lat/Lon values into separate columns.

Current dataframe:

gridReference
(56.37769816725615, -4.325049868061924) 
(56.37769816725615, -4.325049868061924) 
(51.749167440074324, -4.963575226888083)   

wanted dataframe:

Latitude               Longitude
56.37769816725615     -4.325049868061924
56.37769816725615     -4.325049868061924
51.749167440074324    -4.963575226888083 

Thanks for your help

EDIT: I have tried:

df['lat'], df['lon'] = df.gridReference.str.strip(')').str.strip('(').str.split(', ').values.tolist()

but I get the error:

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

I then tried adding:

df['gridReference'] = df['gridReference'].astype('str')

and got the error:

ValueError: too many values to unpack (expected 2)

Any help would be appreciated as I am not sure how to make this work! :)

EDIT: I keep getting the error AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

the output for df.dtypes is:

<class 'pandas.core.frame.DataFrame'> Int64Index: 22899 entries, 0 to 22898 Data columns (total 1 columns): LatLon 22899 non-null object dtypes: object(1)

the output for df.info() is:

gridReference object dtype: object

like image 975
hsquared Avatar asked Mar 11 '23 04:03

hsquared


1 Answers

df['gridReference'].str.strip('()')                               \
                   .str.split(', ', expand=True)                   \
                   .rename(columns={0:'Latitude', 1:'Longitude'}) 

             Latitude           Longitude
0   56.37769816725615  -4.325049868061924
1   56.37769816725615  -4.325049868061924
2  51.749167440074324  -4.963575226888083
like image 70
Nickil Maveli Avatar answered Apr 30 '23 06:04

Nickil Maveli