0

I am trying to combine the longitude and latitude columns so I can use to plot my points with GeoPandas. I tried concatenating the integers using:

df3['Location'] = df3['latitude'].astype(str) + ' ' +      df3['longitude'].astype(str)

But I get the following error:

sequence item 0: expected str instance, float found

I believe this is because the function only takes string.

df3 = pd.DataFrame({'ID':['1','2','3'], 'latitude': [42.14267,42.131203,42.131638], 'longitude':[-76.902040,-76.917860,-76.822420]})

This is my dataFrame now

 ID  latitude       longitude    
0  1      42.142677  -76.902040   
1  2      42.131203  -76.917860   
2  3      42.131638  -76.822420   

But, I want my data frame to look like this. Just a new column, with each row showing the longitude and latitude coordinates separated by a comma and wrapped with parentheses. ex. '(42.142677,-76.902040)'

  ID  latitude       longitude    location
0  1      42.142677  -76.902040   (42.142677,-76.902040)   
1  2      42.131203  -76.917860   (42.131203, -76.917860) 
2  3      42.131638  -76.822420   (42.131638,-76.822420)

Note: I apologize, For some reason the table above is not formatting properly.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Does this answer your question? [Combine two columns of text in pandas dataframe](https://stackoverflow.com/questions/19377969/combine-two-columns-of-text-in-pandas-dataframe) – Rabinzel Sep 25 '22 at 06:27

2 Answers2

0

Convert both the latitude and longitude columns to string first and try to concat later:

df3["latitude"] = df3["latitude"].astype(str)
df3["longitude"] = df3["longitude"].astype(str)

df3["location"] = "(" + df3["latitude"] + ", " + df3["longitude"] + ")"

You can also do it on the fly using the below code:

df3["location"] = "(" + df3["latitude"].astype(str) + ", " + df3["longitude"].astype(str) + ")"
0

To achieve exactly what you have requested. Two columns as a string tuple representation

import pandas as pd
import io

df = pd.read_csv(io.StringIO(""" ID  latitude       longitude    
0  1      42.142677  -76.902040   
1  2      42.131203  -76.917860   
2  3      42.131638  -76.822420"""), sep="\s+")

df["location"] = df.drop(columns=["ID"]).apply(tuple, axis=1).astype(str)

df
ID latitude longitude location
1 42.1427 -76.902 (42.142677, -76.90204)
2 42.1312 -76.9179 (42.131203, -76.91786)
3 42.1316 -76.8224 (42.131638, -76.82242)

For plotting this is not useful. Really you want as geometry

import geopandas as gpd

gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(y=df["latitude"], x=df["longitude"]), crs="epsg:4386")

gdf.explore(marker_kwds={"radius":15}, height=300, width=400)

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30