求和后如何找到最大值

时间:2019-07-18 17:16:22

标签: python pandas

样本数据设置为

alias view="view -n"

我需要获取销量最高的产品名称

.bashrc

但这只是数字,我尝试了

Category         Product Name  Sales       Quantity
Furniture        AAA           100         2
Tecno            DDD           250.56      3
Office Supplies  CCC           278.65      2
household        BBB           957.5775    5
household        BBB           22.368      2
Office Supplies  CCC           48.86       7
Tecno            DDD           7.28        4
Furniture        AAA           907.152     6
Furniture        AAA           18.504      3
Office Supplies  CCC           114.9       5

这也不起作用

答案应该是= CCC

OR

CCC 14

1 个答案:

答案 0 :(得分:0)

您可以使用idxmax获得销量最高的产品:

import pandas as pd

df = pd.DataFrame({
    'Category': ['Furniture', 'Tecno', 'Office Supplies', 'household', 'household',
                 'Office Supplies', 'Tecno', 'Furniture', 'Furniture', 'Office Supplies'],
    'Product Name': ['AAA', 'DDD', 'CCC', 'BBB', 'BBB', 'CCC', 'DDD', 'AAA', 'AAA', 'CCC'],
    'Sales': [100, 250.56, 278.65, 957.5775, 22.368, 48.86, 7.28, 907.152, 18.504, 114.9],
    'Quantity': [2, 3, 2, 5, 2, 7, 4, 6, 3, 5]
})

product_sum = df.groupby('Product Name')['Quantity'].sum()
max_product = product_sum.idxmax()
print(max_product)
# CCC
print(product_sum[max_product])
# 14