first(), last(): First and last values in the group
Grouped Operations
You can apply operations to each group separately using transform() or apply().
Using transform() to alter each group in a group by object
Code
# Transform: apply function to each group, return same-sized DataFramedef normalize(x):return (x - x.mean()) / x.std()df['value_normalized'] = grouped['value'].transform(normalize)
Using apply() to alter each group in a group by object
Code
# Apply: apply function to each group, return a DataFrame or Seriesdef group_range(x):return x['value'].max() - x['value'].min()result = grouped.apply(group_range, include_groups=False)
Pivot Tables
Pivot tables are a powerful tool for reorganizing and summarizing data. They allow you to transform your data from a long format to a wide format, making it easier to analyze and visualize patterns.