I have a dataframe that has Latitude, Longitude, and a unique_id for each row.
df = df[['unique_id','Latitude','Longitude']]
I am using stats.binned_statistic_2d
to create bins that lat/long will correspond with.
stats, yedges, xedges, binnumbers = stats.binned_statistic_2d(
np.array(df['Latitude']),
np.array(
df['Longitude']),
bins=bins,
values=np.array(df['unique_id']),
statistic = 'count'
)
)
But after applying this, I would like to produce another column in the dataframe that shows which bin number the unique_id
is in, where df
will now have df[['unique_id','Latitude','Longitude','binnumber']]
.
Is there an easy pythonic way to do this rather than nesting loops across xedges
and yedges
?
I already tried nesting loops, but that is way too slow and I am sure there is an easier pythonic way to do this.
TIA!