i2_
i2_

Reputation: 735

Add rows according to other rows

My DataFrame object similar to this one:

        Product StoreFrom   StoreTo Date
1   out melon   StoreQ  StoreP  20170602
2   out cherry  StoreW  StoreO  20170614
3   out Apple   StoreE  StoreU  20170802
4   in  Apple   StoreE  StoreU  20170812

I want to avoid duplications, in 3rd and 4th row show same action. I try to reach

        Product StoreFrom   StoreTo Date    Days
1   out melon   StoreQ  StoreP  20170602    
2   out cherry  StoreW  StoreO  20170614    
5   in  Apple   StoreE  StoreU  20170812    10

and I got more than 10k entry. I could not find similar work to this. Any help will be very useful.

Upvotes: 1

Views: 38

Answers (1)

piRSquared
piRSquared

Reputation: 294218

d1 = df.assign(Date=pd.to_datetime(df.Date.astype(str)))
d2 = d1.assign(Days=d1.groupby(cols).Date.apply(lambda x: x - x.iloc[0]))
d2.drop_duplicates(cols, 'last')

    io Product StoreFrom StoreTo       Date    Days
1  out   melon    StoreQ  StoreP 2017-06-02  0 days
2  out  cherry    StoreW  StoreO 2017-06-14  0 days
4   in   Apple    StoreE  StoreU 2017-08-12 10 days

Upvotes: 1

Related Questions