Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get largest difference within a group

Tags:

I'm trying to figure out how to calculate the top 5 products with biggest change in unit sales over prior month. Below is a small slice of my data, here Vendor_SKU and Order_Month are both index created by pd.groupby.

amz = amz.groupby(['Vendor_SKU', 'Order_Month'])['Quantity'].sum()

                          Vendor_SKU  Order_Month
                          DLEBL140    2018-11-01       17.0
                                      2018-12-01       13.0
                          DLEBL90     2018-11-01       29.0
                                      2018-12-01       39.0
                          DLEBR160    2018-11-01       16.0
                                      2018-12-01       17.0
                          DLEG180     2018-11-01       30.0
                                      2018-12-01       20.0
                          DLER150     2018-11-01       22.0
                                      2018-12-01       23.0
                          DLEW110     2018-11-01       49.0
                                      2018-12-01       41.0
                          DLEY130     2018-11-01       32.0
                                      2018-12-01       20.0

What I'd like to achieve is to calculate all difference of the same product and find the ones with the largest difference. Say the result I'm expecting is like:

                  Vendor_SKU  
                  DLEBL140      -4.0
                  DLEBL90       10.0
                  DLEBR160       1.0     
                  DLEG180      -10.0
                  DLER150        1.0
                  DLEW110       -8.0           
                  DLEY130      -12.0

With this result, I can then figure out the top5 changes. Any ideas? Thanks!

Thanks to the quick response from you guys, I tried groupby.diff before posting this question but got a batch of NaN without any index, just a column of NaN with few random numbers. Later I realized that there might be products only got bought on Nov or Dec like the first two rows below, then instead of getting the difference between two months, I only got NaN with diff().

Vendor_SKU Order_Month  Quantity
0          C142  2018-12-01       2.0
1        CC-18P  2018-11-01       5.0
2      DLEBL140  2018-11-01      17.0
3      DLEBL140  2018-12-01      13.0
4       DLEBL90  2018-11-01      29.0
5       DLEBL90  2018-12-01      39.0

Guess I need to insert some rows with the quantity of 0 and then try diff().

like image 925
JT Dong Avatar asked Jan 17 '19 21:01

JT Dong


1 Answers

Start with groupby and diff, since you want the largest diff for each vendor:

amz.groupby(level=0).diff(1).max(level=0)

Vendor_SKU
DLEBL140    -4.0
DLEBL90     10.0
DLEBR160     1.0
DLEG180    -10.0
DLER150      1.0
DLEW110     -8.0
DLEY130    -12.0
Name: Quantity, dtype: float64

From here, if you want to find the top 5 differences, you can use nlargest:

amz.groupby(level=0).diff(1).max(level=0).nlargest(5)

Vendor_SKU
DLEBL90     10.0
DLEBR160     1.0
DLER150      1.0
DLEBL140    -4.0
DLEW110     -8.0
Name: Quantity, dtype: float64
like image 88
cs95 Avatar answered Nov 23 '22 16:11

cs95