After Pandas is aggregated into Series by groupby, it returns the top 2 items with the largest value after sorting (TOP2)

Hope to return the two items of values ​​corresponding to 5 and 4, and their corresponding longitude and latitude values

df_agg1.groupby(['longitude','latitude'])['foot_mark'].sum()

Running the above returns:

longitude latitude
121.258340 37.493010 5
121.259149 37.496940 1
121.259180 37.502860 1
121.264030 37.487900 4
121.302646 37.481054 0
121.308775 37.482152 0
121.315780 37.474968 2
121.347799 37.467399 0
Name: foot_mark, dtype: int64

# 经过sort_values后取top2
df_agg1.groupby(['longitude','latitude'])['foot_mark'].sum().sort_values(ascending=False)[:2]

Running the above returns:

longitude latitude
121.26403 37.48790 1
121.25834 37.49301 1
Name: foot_mark, dtype: int64

How to return the longitude and latitude values?

list(df_agg1.groupby(['longitude','latitude'])['foot_mark'].sum().sort_values(ascending=False)[:2].index)

Running the above returns:

[(121.26403, 37.4879), (121.25834, 37.49301)]

If your problem is solved, welcome to bookmark + like + follow~

Guess you like

Origin blog.csdn.net/weixin_45281949/article/details/106157421