我有一组出租车数据,其中有两列,如下所示:
Neighborhood Borough Time
Midtown Manhattan X
Melrose Bronx Y
Grant City Staten Island Z
Midtown Manhattan A
Lincoln Square Manhattan B基本上,每一行都代表了那个区社区的一辆出租车。现在,我想找出每一个区的前五名,皮卡数量最多的地区。我试过这个:
df['Neighborhood'].groupby(df['Borough']).value_counts()这给了我这样的感觉:
borough
Bronx High Bridge 3424
Mott Haven 2515
Concourse Village 1443
Port Morris 1153
Melrose 492
North Riverdale 463
Eastchester 434
Concourse 395
Fordham 252
Wakefield 214
Kingsbridge 212
Mount Hope 200
Parkchester 191
......
Staten Island Castleton Corners 4
Dongan Hills 4
Eltingville 4
Graniteville 4
Great Kills 4
Castleton 3
Woodrow 1我如何过滤它,使我只从每一个得到前5?我知道有一些类似标题的问题,但它们对我的情况没有帮助。
发布于 2019-12-25 20:17:45
df['Neighborhood'].groupby(df['Borough']).value_counts().head(5)head()获取数据帧中的前5行。
https://stackoverflow.com/questions/35364601
复制相似问题