coder_bg
coder_bg

Reputation: 370

How to find the gap of days between the last sale in Pandas?

I have a dataframe like :

Date | RtrID | PrdId| Qty
2020-03-30 |    234 |   HECFR175-A0 |   1
2020-03-30 |    234 |   HECFR175-A0 |   1
2020-03-30 |    234 |   HECFR175-A0 |   1
2020-04-1 | 234 |   HECFR175-A0 |   1
2020-04-1 | 234 |   HECFR175-A0 |   1
2020-04-1 | 234 |   HECFR175-A0 |   1

The days I want is in the combination of RtrID and PrdId i.e here Rtr ID of 234 with PrdId of HECFR175-A0 has the GAP of 1 day since the last purchase.

I tried grouped =

final_df.groupby(['PrdCde', 'RtrId'])
gap_in_days_from_sale_Dlv = grouped.SalDlvDate.apply(lambda x: x.diff())

but I am getting result in a negative way like -25 , -20 and Nat's. What should I do?

Upvotes: 0

Views: 72

Answers (1)

wwnde
wwnde

Reputation: 26676

If you needed it against each row, groupby transform the difference between max and min Date

df['diff']=df.groupby(['PrdId','RtrID'])['Date'].transform(lambda x:x.max()-x.min())

If all you need is a value then apply lambda will help

df.groupby(['PrdId','RtrID'])['Date'].apply(lambda x:x.max()-x.min())

Upvotes: 1

Related Questions