Python: Drop Pandas row if function returns false, utilizing is _land check through Cartopy
Python: Drop Pandas row if function returns false, utilizing is _land check through Cartopy
我想根据函数 returns 是否为真来删除一行,该函数检查纬度和经度值是否在陆地上。我想删除 lat/long returns 错误的行。
这是我目前所拥有的,但我卡住了:
def LAND_SHAPE_READER():
land_shp_fname = shpreader.natural_earth(resolution='10m',
category='physical',
name='land')
land_geom = unary_union(list(shpreader.Reader(land_shp_fname).geometries()))
land = prep(land_geom)
def IS_LAND(x, y):
return land.contains(sgeom.Point(x, y))
def MAP_PLOT_CHECK():
if IS_LAND(df['Longitude'], df['Latitude']):
return
else:
#drop row here
我认为最安全的方法是创建一个新列来存储您的 IS_LAND 值。
# Apply this function to every row, where x is the row
# Save the True/False return value as a column
df['IS_LAND_RETURN'] = df.apply(lambda x: IS_LAND(x['Longitude'], x['Latitude']), axis=1)
之后过滤就很简单了:
# Select df rows where IS_LAND_RETURN is true
land_only = df[df['IS_LAND_RETURN']]
我想根据函数 returns 是否为真来删除一行,该函数检查纬度和经度值是否在陆地上。我想删除 lat/long returns 错误的行。
这是我目前所拥有的,但我卡住了:
def LAND_SHAPE_READER():
land_shp_fname = shpreader.natural_earth(resolution='10m',
category='physical',
name='land')
land_geom = unary_union(list(shpreader.Reader(land_shp_fname).geometries()))
land = prep(land_geom)
def IS_LAND(x, y):
return land.contains(sgeom.Point(x, y))
def MAP_PLOT_CHECK():
if IS_LAND(df['Longitude'], df['Latitude']):
return
else:
#drop row here
我认为最安全的方法是创建一个新列来存储您的 IS_LAND 值。
# Apply this function to every row, where x is the row
# Save the True/False return value as a column
df['IS_LAND_RETURN'] = df.apply(lambda x: IS_LAND(x['Longitude'], x['Latitude']), axis=1)
之后过滤就很简单了:
# Select df rows where IS_LAND_RETURN is true
land_only = df[df['IS_LAND_RETURN']]