Skip to content

Instantly share code, notes, and snippets.

@lgmoneda
Created November 14, 2019 17:19
Show Gist options
  • Save lgmoneda/76ca2de77d8ba22850b7e2c58874320c to your computer and use it in GitHub Desktop.
Save lgmoneda/76ca2de77d8ba22850b7e2c58874320c to your computer and use it in GitHub Desktop.
Using named aggregation in Pandas
import pandas as pd
import numpy as np
data = [[1, 5, 2], [2, 3, 1], [3, 1, 1], [1, 3, 2]]
df = pd.DataFrame(data, columns=["values_1", "values_2", "age"])
agg_columns = ["values_1", "values_2"]
agg_functions = [np.mean, np.std]
agg_dict = {col + "_" + f.__name__: (col, f) for col in agg_columns for f in agg_functions}
agg = df.groupby("age").agg(**agg_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment