[英]How can I sum two different columns at once where one contains Decimal objects in pandas?
我有一個數據框,我想匯總兩個不同列的總和。 這是我原始數據幀的df.head(5)
。
price name quantity transaction_amount
pk
48 1.00 Product 1 1 1.00
48 1.00 Product 1 4 4.00
63 1.00 Product 2 2 2.00
63 1.00 Product 2 3 3.00
63 1.00 Product 2 1 1.00
我想按pk
分組它們, pk
是產品的數據庫主鍵,並獲取transaction_amount
列和quantity
數量列的總和。 但是當我執行df.groupby(['pk', 'name']).sum()
我得到了:
quantity
pk name
48 Product 1 543
63 Product 2 17234
38 Product 3 4014
39 Product 4 11053
40 Product 5 13406
transaction_amount
列在哪里? transaction_amount
是transaction_amount
中的quantity
和該項目在該交易中的price
。 如果有折扣或其他折扣,這可以更改每筆交易。 我們需要記錄購買時對物品收取的費用。 所以我想要的結果將是quantity
(總數量), transaction_amounts
(總數量), name
和pk
如下所示:
quantity transaction_amount
pk name
48 Product 1 543 543.00
63 Product 2 17234 89,000.93
38 Product 3 4014 2,000.32
39 Product 4 11053 25,000.36
40 Product 5 13406 6,000.12
我閱讀了.sum()
的文檔 ,但所有選項都不適合我。 如果我刪除price
列並運行.sum(level=0)
則需要很長時間。 看一下這兩種不同方法的時間(速度越快,它只會對“ quantity
列求和)。
In [237]: %%timeit
...: df.groupby(['pk', 'name']).sum(level=0)
...:
1 loop, best of 3: 3.04 s per loop
In [239]: %%timeit
...: df.groupby(['pk', 'name']).sum()
...:
...:
10 loops, best of 3: 42.4 ms per loop
.sum(axis=1)
的結果也相似。
當我跑步
df.groupby(['pk', 'name']).sum()
我懂了
price quantity transaction_amount
pk name
48 Product 1 2.0 5 5.0
63 Product 2 3.0 6 6.0
這向我表明您的price
和transaction_amount
是對象。
由於您使用的是decimal.Decimal
對象,因此numpy.sum
不會處理您的對象。 因此,只需遵照內置的sum
:
In [18]: df
Out[18]:
pk price name quantity transaction_amount
0 48 1.0 Product 1 1 1.0
1 48 1.0 Product 1 4 4.0
2 63 1.0 Product 2 2 2.0
3 63 1.0 Product 2 3 3.0
4 63 1.0 Product 2 1 1.0
In [19]: df.groupby(['pk', 'name']).aggregate({
...: "quantity":np.sum,
...: "price":sum,
...: "transaction_amount":sum
...: })
Out[19]:
price quantity transaction_amount
pk name
48 Product 1 2.0 5 5.0
63 Product 2 3.0 6 6.0
注意,這會很慢,但這是使用object
dtype列必須支付的價格。
您可以像這樣指定要累加的列。
df.groupby(['pk','name'])['quantity','transaction_amount'].sum()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.