gpt4 book ai didi

python - 排序 Pandas DataFrame

转载 作者:太空狗 更新时间:2023-10-29 22:21:10 24 4
gpt4 key购买 nike

我有一个包含汽车数据的 Pandas Dataframe。我想为每个制造商找到最畅销的两个模型,然后对制造商进行降序排列。

Maker   Model   No Sold(,000s)
Ford Kuga 35
Ford Focus 47
Ford Ka 31
Ford Fiesta 68
Ford Mondeo 55
Ford S-Max 34
Ford Galaxy 23
Nissan Leaf 28
Nissan Micra 31
Nissan Note 43
Nissan Pulsar 23
Nissan Juke 57
Nissan Qashqai 62
Nissan X-Trail 38
Honda Jazz 24
Honda Civic 32
Honda HRV 33
Honda CRV 29
Honda Accord 30
Honda NSX 15
Toyota Aygo 44
Toyota Auris 45
Toyota Avensis 35
Toyota Prius 32
Toyota Rav4 29
Toyota Land Cruiser 14
Citroen C1 40
Citroen C3 25
Citroen C4 46
Citroen DS3 35
Citroen DS4 31
Citroen DS5 25
Audi A1 23
Audi A3 47
Audi A4 30
Audi A6 20
Audi A8 18
BMW 1 Series 36
BMW 2 Series 20
BMW 3 Series 53
BMW 4 Series 21
BMW 5 Series 27
BMW 6 Series 24
BMW 7 Series 16

抱歉,不确定如何将 Dataframe 放在这里。

最佳答案

使用groupby + nlargest

df.set_index('Model').groupby('Maker')['No Sold(,000s)'].nlargest(2)

Maker Model
Audi A3 47
A4 30
Citroen C4 46
C1 40
Ford Fiesta 68
Mondeo 55
Honda HRV 33
Civic 32
Nissan Qashqai 62
Juke 57
Toyota Auris 45
Aygo 44
Name: No Sold(,000s), dtype: int64

关于python - 排序 Pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42182833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com