簡體   English   中英

根據 python dataframe 中的特定條件減去特定列的行值

[英]Subtracting values of a row for a specific column based on a specific condition in python dataframe

我的數據如下所示:

Customer  Product Date       Amount Paid
C1        P1      5/10/2011  100
C1        P1      5/18/2015  200
C1        P1      6/17/2019  300
C2        P2      4/18/2019  50

我想要為每個客戶和產品,根據日期支付的最后兩個金額之間的差異,第一個和最后一個支付金額之間的差異。 以及支付的最高和最低金額之間的差額。

對於只有一筆交易的客戶,這些變為 0。因此 output 應如下所示:

Customer Product   Diff_first_last    Diff_last_two   Diff_min_max
C1       P1        200                100             200
C2       P2        0                  0                0

這是apply通行證的一種方式

df.groupby(['Customer','Product']).Amount.apply(lambda x : pd.Series({'Diff_first_last':x.iloc[0]-x.iloc[-1],
                                                                      'Diff_last_two':x.iloc[-2:].diff().fillna(0).iloc[-1],
                                                                      'Diff_min_max':np.ptp(x)})).unstack()
                  Diff_first_last  Diff_last_two  Diff_min_max
Customer Product                                              
C1       P1                -200.0          100.0         200.0
C2       P2                   0.0            0.0           0.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM