Skip to content Skip to sidebar Skip to footer

Compute The Nth Day From The First Event Within Each Group In Pandas

This is a follow-up question from my other question: I have the following data frame, subsetted from my original data frame, with columns ob, event, unixtime, and day, and I want

Solution 1:

In [46]: firstdays = df.groupby('ob').day.first()

In [47]: firstdays
Out[47]: 
ob
a     2012-09-03
b     2012-09-04
c     2012-09-03
d     2012-09-03
e     2012-09-01
f     2012-09-02
Name: day

In [48]: df.apply(lambda row: (row['day'] - firstdays[row['ob']]).days + 1, axis=1)
Out[48]: 
343340    1
343341    1
343342    1
343343    5
343344    1
343345    1
343349    1
343350    1
343351    1
343352    1
343353    5
343354    8

Post a Comment for "Compute The Nth Day From The First Event Within Each Group In Pandas"