【问题标题】:Python: Drop Pandas row if function returns false, utilizing is _land check through CartopyPython:如果函数返回 false,则删除 Pandas 行,通过 Cartopy 使用 is _land 检查
【发布时间】:2018-11-05 20:27:59
【问题描述】:

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

这是我目前所拥有的,但我卡住了:

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

【问题讨论】:

    标签: python python-3.x pandas cartopy


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这应该可行,老实说,我想多了。
    猜你喜欢
    • 2018-04-16
    • 2015-04-22
    • 2012-08-19
    • 2019-12-17
    • 1970-01-01
    • 2015-05-14
    • 2014-02-02
    • 2016-11-22
    • 1970-01-01
    相关资源
    最近更新 更多