I have a dataframe that looks like:
lon lat
-77.487 39.044
-77.487 39.044
-122.031 37.354
-77.487 39.044
I want to group these lon-lat pairings with a resulting count, like so:
lon lat count
-77.487 39.044 3
-122.031 37.354 1
How can I do this? The group()
function only appears to allow for grouping by one column.
You could use groupby.size
and rename the column created followed by reset_index
to get back the desired dataframe
.
print(df.groupby(['lon', 'lat']).size().rename('count').reset_index())
lon lat count
0 -122.031 37.354 1
1 -77.487 39.044 3
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments