I have an example DataFrame like the following:
import pandas as pd
import numpy as np
df = pd.DataFrame({'ID':[1,2,2,2,3,3,], 'date':array(['2000-01-01','2002-01-01','2010-01-01','2003-01-01','2004-01-01','2008-01-01'],dtype='datetime64[D]')})
I am trying to get the 2nd earliest day in each ID group. So I wrote the following funciton:
def f(x):if len(x)==1:return x[0]else:x.sort()return x[1]
And then I wrote:
df.groupby('ID').date.apply(lambda x:f(x))
The result is an error.
Could you find a way to make this work?