我有一個看起來像這樣的資料框:
Fruit Quantity
orange 4
grape 2
apple 3
grape 2
orange 1
我想根據水果列中的相同專案名稱總結數量列。想要的結果是:
Fruit Quantity
orange 5
apple 3
grape 4
uj5u.com熱心網友回復:
價值計數是最好的方法
df.groupby('Fruit').sum()
uj5u.com熱心網友回復:
來自@GevorgAtanesyan 和@db 的答案完美無缺( 1)。
但是,要獲得 aDataFrame而不是 aSerie作為輸出,可以使用此表示法:
>>> df.groupby('Fruit')['Quantity'].sum().reset_index()
Fruit Quantity
0 apple 3
1 grape 4
2 orange 5
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/344583.html
上一篇:通過布爾函式過濾熊貓資料框
下一篇:繪圖,按標簽,詞頻
