Skip to content Skip to sidebar Skip to footer

Using `.groupby().apply()` Instead Of `.groupby().agg()`

Suppose I have a dataframe like this d = {'User':['A', 'A', 'B'], 'time':[1,2,3], 'state':['CA', 'CA', 'OR'], 'type':['cd', 'dvd', 'cd']} df = pd.Dataframe(data=d)

Solution 1:

IIUC,

def aggUser(df):
    a = pd.DataFrame({'avg_time':df['time'].mean(), 
                      'state': [df['state'].iloc[0]]})
    b = df['type'].value_counts().to_frame().T.reset_index(drop=True)
    return pd.concat([a,b], axis=1).set_axis(df['User'].iloc[[0]])

pd.concat([aggUser(df.query('User == "A"')),
           aggUser(df.query('User == "B"'))])

Output:

      avg_time state  cd  dvd
User                         
A1.5    CA   11.0B3.0    OR   1  NaN

df.groupby('User', group_keys=False).apply(aggUser)

Output:

      avg_time state  cd  dvd
User                         
A1.5    CA   11.0B3.0    OR   1  NaN

Post a Comment for "Using `.groupby().apply()` Instead Of `.groupby().agg()`"