0

I have an image of a land area with certain counts at different longs and lats. I want to sum up the counts which fall in water within that area (the water has a specific color). I'm uncertain on how to do this. This is what I have so far:

latbounds = (min, max)
lonbounds = (min, max)
newlist = []
count=[]
for i, row in df.iterrows():
    if row['Latitude'] >= latbounds[0] and row['Latitude'] <= latbounds[1]:
        if row['Longitude'] >= lonbounds[0] and row['Longitude'] <= lonbounds[1]:
            newlist.append((row['Longitude'], row['Latitude']))
            count.append((row['Count']))
len(count)
CountTot= {}

for i in zip(newlist,count):
    try:
        CountTot[i[0]] = int(i[1]) + int(CountTot[i[0]])
    except KeyError:
        CountTot[i[0]] = int(i[1])
#for k,v in CountTot.items():
Position=list(CountTot.keys())
NumCount=list(CountTot.values())


x=[]
y=[]
fig, ax = plt.subplots()
ax.imshow(w, extent=[43,44,-80,-79])
for i in Position:
    x.append(i[0])
    y.append(i[1])
    ax.plot(x,y, "o", color="red")
fig
Barmar
  • 741,623
  • 53
  • 500
  • 612
aleucid
  • 11
  • 2
  • Why don't you add to `CountTot` in the first loop, instead of using another loop? – Barmar Jul 24 '21 at 18:26
  • 1
    What does your dataframe look like? `df.iterrows` is usually [not the best way to work with Pandas](https://stackoverflow.com/a/55557758/15873043).. – fsimonjetz Jul 24 '21 at 18:27
  • 2
    And can't you just do this directly in pandas, instead of using `iterrows()`? `df[min <= df['Latitude'] <= max & min <= df['Longitude'] <= max]['Count']`? – Barmar Jul 24 '21 at 18:27

0 Answers0