Search code examples
pythonmappingexclude-constraint

How to plot lat/long points but exclude some based on another variable?


I'm using python to plot a fish population in a certain area and my lat/long coordinates indicate every trawl while recording where the species is/isn't. I want to plot all of my coordinates where the species is by excluding the coordinates where the abundance of the species is zero. Any ideas?

This is my script right now. I expected it to show all of the coordinates which it does but I dont know how to exclude the zeroes.

dt = pd.read_csv(data)
fig = px.scatter_geo(dt,lat='Lat',lon='Lon', hover_name="Abundance")
fig.update_layout(title = 'World Map: plot fish and hover to see abundance', title_x=0.5)
fig.show()

Solution

  • This should solve your issue.

    dt = pd.read_csv(data)
    
    dt = dt[dt['Abundance']>0]
    
    fig = px.scatter_geo(dt,lat='Lat',lon='Lon', hover_name="Abundance")
    fig.update_layout(title = 'World Map: plot fish and hover to see abundance', title_x=0.5)
    fig.show()
    
    dt['Abundance']>0
    

    finds all entries where the abundance is larger than 0 and returns it as a Series of boolean results.

    dt[dt['Abundance']>0]
    

    Selects all the data according to the resulting Series. Try to play with this since it is a very powerful tool to filter your data. (Numpy arrays can be filtered in the same way btw.)