Skip to content Skip to sidebar Skip to footer

Looping Into Dates And Apply Function To Pandas Dataframe

I'm trying to detect the first dates when an event occur: here in my dataframe for the product A (see pivot table) I have 20 items stored for the first time on 2017-04-03. so I wan

Solution 1:

Let's try this:

df1['age'] = df1.groupby('name')['age'].transform(lambda x: (x==x.min())*x)
df1.pivot_table(index='name', columns='date', values='age').replace(0,np.nan)


date  2017-03-30  2017-03-31  2017-04-03  2017-04-04
name                                                
A            NaN        20.0         NaN         NaN
B           10.0         NaN         NaN         NaN

Post a Comment for "Looping Into Dates And Apply Function To Pandas Dataframe"