Skip to content

Instantly share code, notes, and snippets.

@Abuton
Created March 1, 2021 19:53
Show Gist options
  • Save Abuton/9f20b871f7f6659e35249e16845853ee to your computer and use it in GitHub Desktop.
Save Abuton/9f20b871f7f6659e35249e16845853ee to your computer and use it in GitHub Desktop.
# function to calculate feature engineering by aggregating integer columns
def get_agg_by_cols(data, col, agg_list, agg_cols):
"""
data : dataframe to calculate FE on
col : list of columns to aggregate by (the object datatype)
agg_list : a list of statistical measure e.g mean, median
agg_cols : a list of numerical datatype columns
"""
for cols in col:
for i in agg_list:
for j in agg_cols:
data[cols+'_'+j+'_'+i] = data.groupby([cols])[j].transform(i)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment