我有以下資料框:
import pandas as pd
df_coords = pd.DataFrame({'lat': {'1010': '37_N',
'1050': '32_N',
'1059': '19_N',
'1587': '6_S',
'3367': '44_N'},
'lon': {'1010': '65_W',
'1050': '117_W',
'1059': '156_W',
'1587': '106_E',
'3367': '12_E'}})
我正在嘗試轉換這些坐標,以便我可以構建一個GeoPandas資料框物件,但我似乎無法弄清楚如何轉換字串。
import geopandas as gpd
gpd.points_from_xy(x=["37_N"], y=["65_W"])
# ---------------------------------------------------------------------------
# ValueError Traceback (most recent call last)
# <ipython-input-36-a02c9f8a011d> in <module>
# ----> 1 gpd.points_from_xy(x=["37_N"], y=["65_W"])
# ~/anaconda3/envs/soothsayer_py3.8_env/lib/python3.8/site-packages/geopandas/array.py in points_from_xy(x, y, z, crs)
# 256 output : GeometryArray
# 257 """
# --> 258 return GeometryArray(vectorized.points_from_xy(x, y, z), crs=crs)
# 259
# 260
# ~/anaconda3/envs/soothsayer_py3.8_env/lib/python3.8/site-packages/geopandas/_vectorized.py in points_from_xy(x, y, z)
# 241 def points_from_xy(x, y, z=None):
# 242
# --> 243 x = np.asarray(x, dtype="float64")
# 244 y = np.asarray(y, dtype="float64")
# 245 if z is not None:
# ~/anaconda3/envs/soothsayer_py3.8_env/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
# 83
# 84 """
# ---> 85 return array(a, dtype, copy=False, order=order)
# 86
# 87
# ValueError: could not convert string to float: '37_N'
誰能描述如何將N,S,W,E資訊轉換為與實際坐標相關的資訊GeoPandas?
uj5u.com熱心網友回復:
您需要將您的值映射到-180 and 180經度和緯度之間的范圍內。這里是 geopandas 檔案
這是一個執行此操作的函式。
def convert_value(value):
if value.endswith('_N') or value.endswith('_E'):
return int(value[:-2])
if value.endswith('_S') or value.endswith('_W'):
return -int(value[:-2])
輸入要轉換的值,例如“12_W”。該函式將回傳“-12”。不能保證什么值應該是負數,什么應該是正數。我只是在最有意義的地方設定了減號。
geopandas.points_from_xy(value_longitude, value_latitude, crs="EPSG:4326")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/420939.html
標籤:
上一篇:計數值在列中連續出現的次數
下一篇:將長資料重塑為多個寬列
