首页 > 解决方案 > 熊猫排序值以获取最多放置的项目

问题描述

如何从该数据中显示哪个项目放置的数量最多?

如何显示哪个项目是最有序的 groupby 选择描述?

url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv'
df= pd.read_csv(url, sep = '\t')

我的数据

order_id    quantity    item_name   choice_description  item_price
1   1   Chips and Fresh Tomato Salsa    NULL    $2.39 
1   1   Nantucket Nectar    [Apple] $3.39 
2   2   Chicken Bowl    [Tomatillo-Red Chili Salsa (Hot), [Black Beans, Rice, Cheese, Sour Cream]]  $16.98 
3   1   Chicken Bowl    [Fresh Tomato Salsa (Mild), [Rice, Cheese, Sour Cream, Guacamole, Lettuce]] $10.98 
3   1   Side of Chips   NULL    $1.69 
4   1   Steak Burrito   [Tomatillo Red Chili Salsa, [Fajita Vegetables, Black Beans, Pinto Beans, Cheese, Sour Cream, Guacamole, Lettuce]]  $11.75 
4   1   Steak Soft Tacos    [Tomatillo Green Chili Salsa, [Pinto Beans, Cheese, Sour Cream, Lettuce]]   $9.25 
...
...

标签: pythonpandas

解决方案


  • 如果要显示所有数据:
df.sort_values('quantity', ascending=False)

输出:

order_id    quantity    item_name                     choice_descri item_price
1443        15          Chips and Fresh Tomato Salsa  NaN            $44.25
1660        10          Bottled Water                 NaN            $15.00
1559        8           Side of Chips                 NaN            $13.52
1443        7           Bottled Water                 NaN            $10.50
...
  • 如果您只想显示第一行:
df.sort_values('quantity', ascending=False).head(1)

输出:

order_id    quantity    item_name                     choice_descri item_price
1443        15          Chips and Fresh Tomato Salsa  NaN            $44.25
  • 或者,如果您只想显示名称:
df.sort_values('quantity', ascending=False).head(1).item_name
3598    Chips and Fresh Tomato Salsa
Name: item_name, dtype: object

推荐阅读