Python 如何找出每种产品在熊猫数据框中销售了多少

Python 如何找出每种产品在熊猫数据框中销售了多少,python,pandas,dataframe,Python,Pandas,Dataframe,我有这样一个熊猫数据框: Name Product Amount 0 Bob Apple 1 1 Bob Banana 2 2 Jessica Orange 3 3 Jessica Banana 4 4 Jessica Tomato 3 5 Mary Banana 2 6 John Apple 3 7 John Grape

我有这样一个熊猫数据框:

      Name Product  Amount
0      Bob   Apple       1
1      Bob  Banana       2
2  Jessica  Orange       3
3  Jessica  Banana       4
4  Jessica  Tomato       3
5     Mary  Banana       2
6     John   Apple       3
7     John   Grape       1
到目前为止我所做的:

l = []
count=0
for i in range(0,8):
    row = df.iloc[i]
    
    if row.Product not in l:
        l.append(row.Product)

现在,l包含Product列中的所有唯一值,但我还需要总量

我怎样才能知道每种产品售出了多少件物品?例如,售出了4台苹果?

您正在寻找的产品。groupby功能:

print( df.groupby('Product')['Amount'].sum() )
印刷品:

Product
Apple     4
Banana    8
Grape     1
Orange    3
Tomato    3
Name: Amount, dtype: int64
4 of Apple were sold.
印刷品:

Product
Apple     4
Banana    8
Grape     1
Orange    3
Tomato    3
Name: Amount, dtype: int64
4 of Apple were sold.
您正在查找。groupby函数:

print( df.groupby('Product')['Amount'].sum() )
印刷品:

Product
Apple     4
Banana    8
Grape     1
Orange    3
Tomato    3
Name: Amount, dtype: int64
4 of Apple were sold.
印刷品:

Product
Apple     4
Banana    8
Grape     1
Orange    3
Tomato    3
Name: Amount, dtype: int64
4 of Apple were sold.

你是最棒的!非常感谢。你是最棒的!谢谢。