【发布时间】:2019-06-01 17:45:29
【问题描述】:
我正在尝试将一列添加到 Geopandas (0.4.0) 中的地理数据框中,其中包含来自 geoseries 的单个值(点)以用于进一步计算。
但是,在简单地创建一个新列并直接分配 geoseries 之后,我注意到新列填充了 NaN。
如果我使用 shapely 对象本身,我会收到以下错误消息: 'AssertionError: 新值的形状必须与管理器形状兼容'
下面的例子:
import pandas as pd
import numpy as np
import geopandas as gpd
from shapely.geometry import Point
# create some geometry
coordinates = {'lng': [1,2,3], 'lat': [4,5,6], 'loc': ['a', 'b', 'd']}
df = pd.DataFrame(coordinates, columns = ['loc', 'lat', 'lng'])
df['geometry'] = df.apply(
lambda x: Point((x.lat, x.lng)),
axis = 1)
# create point of interest
coordinates_center = {'lng': 2.2, 'lat': 4.8, 'loc': ['c']}
df_center = pd.DataFrame(coordinates_center)
df_center['geometry'] = df.apply(
lambda x: Point((x.lat, x.lng)),
axis = 1)
# check data type
print (type(df_center))
center = df_center['geometry']
print (type(center))
center_point = center[0]
print (type(center_point))
#create new column in main dataframe and assign the point of interest
df.assign(center=center_point)
【问题讨论】:
-
gdf = gdf.assign(new_column=new_value) -
您好,求帮助。在 new_column 中仍然只有 nan。我试图分配的值是一个匀称的点
-
你能提供一些可重现的代码示例吗?我们看不到的东西很难调试。
-
添加代码,抱歉
标签: python-3.x geopandas