pandas对值进行排序,以获取放置数量最多的项目

b09cbbtk  于 2023-05-05  发布在  其他
关注(0)|答案(3)|浏览(145)

如何显示该数据中放置数量最多的项目?
如何显示哪个项目排序最多groupby choice_description?

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 
...
...
v1uwarro

v1uwarro1#

  • 如果您想显示所有数据:
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
3bygqnnd

3bygqnnd2#

这将列出所有关系(如果有)。

df.loc[df["quantity"] == df["quantity"].max(), "item_name"]

输出:

3598    Chips and Fresh Tomato Salsa
Name: item_name, dtype: object
r8uurelv

r8uurelv3#

数量最多的项目-chipo.groupby(['item_name'])['quantity'].sum().sort_values(ascending=False).index.values[0]
按选择排序最多的组-chipo.groupby(['choice_description'])['quantity'].sum().sort_values(ascending=False).index.values[0]

相关问题