Created
November 14, 2019 17:19
-
-
Save lgmoneda/76ca2de77d8ba22850b7e2c58874320c to your computer and use it in GitHub Desktop.
Using named aggregation in Pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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