Pandas dataframes are great for storing non-homogeneous data types, and the groupby method adds a lot of flexibility for aggregating the data; however, I often would like to keep underlying data that can't be aggregated after performing the groupby operation.
Example:
Let's work with the following dataframe:
df_cars = pd.DataFrame({
'brand': ['Porsche', 'Porsche', 'Porsche', 'BMW' ,'BMW' ,'BMW'],
'model': ['911 GT3', '911', '911', 'M3', 'M3', 'M3', ],
'speed(mph)': [100, 120, 110, 70, 80, 90]})
If I were to run the following code:
df_cars.groupby('brand').mean()
I would get the mean speed(mph) on a per-brand basis; however, I would lose the underlying data from the model and brand columns.
In order to keep that information and perform the same mean calculation I would need to run the following code instead:
df_cars.groupby('brand').agg({
'speed(mph)':'mean',
'model': lambda x: x.iloc[0] if x.nunique() == 1 else list(x),
'brand': lambda x: x.iloc[0] if x.nunique() == 1 else list(x)
})
This returns a dataframe with the mean calculated on a per-brand basis and keeps the underlying data (now collapsed) in the resulting dataframe as well.
Is this the most efficient way to retain this information? Furthermore, why is this not an option in the default implementation of groupby in order to more easily retain data that can't be aggregated?