Python:如果函数返回false,则删除Pandas行,通过Cartopy使用_land检查

时间:2018-11-05 20:27:59

标签: python python-3.x pandas cartopy

我想根据函数是否返回true来删除一行,该函数检查纬度和经度值是否在陆地上。我想删除纬度/经度返回false的行。

这是我到目前为止所遇到的但我卡住了:

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

1 个答案:

答案 0 :(得分:0)

我认为最安全的方法是创建一个存储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']]