Reputation: 680
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.
Upvotes: 3
Views: 1581
Reputation: 9711
Please find the documentation from the following link
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html
df_data=df_data.groupby(['lon','lat']).size()
print df_data
Upvotes: 1
Reputation: 29711
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
Upvotes: 6