[英]How do I convert coordinates in WKT format to columns of Latitude and Longitude
I have extracted co-ordinates data from Twitter. The points I got are in the WKT format.我从Twitter中提取坐标数据。我得到的点是WKT格式。 I want to split them into two columns of Latitude and Longitude.
我想将它们分成纬度和经度两列。 The format of the coordinates is
坐标格式为
{'type': 'Point', 'coordinates': [77.58168, 12.8952]}
{'type': 'Point', 'coordinates': [77.64363, 12.9739]}
{'type': 'Point', 'coordinates': [75.9372318, 12.44627712]}
{'type': 'Point', 'coordinates': [77.5945627, 12.9715987]}
{'type': 'Point', 'coordinates': [77.53584257, 13.05144109]}
{'type': 'Point', 'coordinates': [77.5945627, 12.9715987]}
{'type': 'Point', 'coordinates': [77.58721, 12.96643]}
I removed all the punctuations and unwanted text using str.replace我使用 str.replace 删除了所有标点符号和不需要的文本
df['coordinates'] = df.coordinates.str.replace('type,?' , '')
df['coordinates'] = df.coordinates.str.replace('Point,?' , '')
df['coordinates'] = df.coordinates.str.replace('coordinates,?' , '')
df['coordinates'] = df.coordinates.str.replace('{,?' , '')
df['coordinates'] = df.coordinates.str.replace(',,?' , '')
df['coordinates'] = df.coordinates.str.replace(':,?' , '')
df['coordinates'] = df.coordinates.str.replace('],?' , '')
df['coordinates'] = df.coordinates.str.replace(',?' , '')
df['coordinates'] = df.coordinates.str.replace('},?' , '')
df['coordinates'] = df.coordinates.str.replace("'',?" , "")
I tried to split the column using我尝试使用拆分列
df = pd.DataFrame(df.coordinates.str.split(' ',1).tolist(),
columns = ['Long','Lat'])
But it's not working.但它不起作用。 Please let me know what can be done to convert WKT to Columns of Co-ordinates
请让我知道如何将 WKT 转换为坐标列
The geopandas
docs covers importing data in WKT format using shapely
. geopandas
文档介绍了使用shapely
以 WKT 格式导入数据。 In their example, given your dataframe df
, you could try:在他们的示例中,给定您的 dataframe
df
,您可以尝试:
from shapely import wkt
import geopandas as gpd
df['coordinates'] = df['coordinates'].apply(wkt.loads)
gdf = gpd.GeoDataFrame(df, geometry='coordinates')
If you want, you can get the latitude and longtitude and assign back to your original dataframe by doing the following.如果需要,您可以通过执行以下操作获取纬度和经度并将其分配回原始 dataframe。
df['lat'] = gdf.geometry.x
df['long'] = gdf.geometry.y
Since you didn't provide a complete minimum working example, I haven't tested this code, but I think it should work.由于您没有提供完整的最低工作示例,我还没有测试这段代码,但我认为它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.