如果地理坐标在国家 shapefile 之外,则删除 Pandas 数据框行
Drop Pandas dataframe rows if geocoordinates outside country shapefile
我正在编写一个脚本,该脚本将过滤并删除任何包含特定国家/地区 shapefile 之外的坐标的行。
这是我目前所拥有的
import pandas as pd
import shapefile
from shapely.geometry import Point
from shapely.geometry import shape
df = pd.read_stata(r'C:PathtoDataFrame')
shp = shapefile.Reader(r'C:PathtoShapeFile')
all_shapes = shp.shapes()
all_records = shp.records()
def InCountry(lo,la):
point_to_check = (lo, la)
for i in range(len(all_shapes)):
boundary = all_shapes[i]
if Point(point_to_check).within(shape(boundary)):
#Inside country shape file
return (1)
else:
#Outside country shape file
#return (0) this doesn't work use pass instead
pass
#Create copy of dataframe with only incountry coordinates
df_copy = df.apply(InCountry(df.geopointlongitude, df.geopointlatitude))
然而,我不断收到
TypeError: cannot convert the series to <class 'float'> error
当我运行脚本时。
我该如何解决这个问题?
如何让 InCountry 函数正确过滤地理坐标。
如有任何关于如何改进脚本的建议,我们将不胜感激。
为避免 TypeError,请尝试按如下方式替换创建数据帧副本的代码:
df_copy = df.apply(lambda x: InCountry(x.geopointlongitude, x.geopointlatitude), axis=1)
我正在编写一个脚本,该脚本将过滤并删除任何包含特定国家/地区 shapefile 之外的坐标的行。
这是我目前所拥有的
import pandas as pd
import shapefile
from shapely.geometry import Point
from shapely.geometry import shape
df = pd.read_stata(r'C:PathtoDataFrame')
shp = shapefile.Reader(r'C:PathtoShapeFile')
all_shapes = shp.shapes()
all_records = shp.records()
def InCountry(lo,la):
point_to_check = (lo, la)
for i in range(len(all_shapes)):
boundary = all_shapes[i]
if Point(point_to_check).within(shape(boundary)):
#Inside country shape file
return (1)
else:
#Outside country shape file
#return (0) this doesn't work use pass instead
pass
#Create copy of dataframe with only incountry coordinates
df_copy = df.apply(InCountry(df.geopointlongitude, df.geopointlatitude))
然而,我不断收到
TypeError: cannot convert the series to <class 'float'> error
当我运行脚本时。
我该如何解决这个问题?
如何让 InCountry 函数正确过滤地理坐标。
如有任何关于如何改进脚本的建议,我们将不胜感激。
为避免 TypeError,请尝试按如下方式替换创建数据帧副本的代码:
df_copy = df.apply(lambda x: InCountry(x.geopointlongitude, x.geopointlatitude), axis=1)