简体   繁体   中英

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

My data looks like below:

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

I want for each customer and product, difference between the last two amount paid based on the date,difference between 1st and last amount paid. and difference between and maximum and minimum amount paid.

For customers who have only one transaction, these becomes 0. So output should look like:

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

Here is one way pass to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM