Skip to content Skip to sidebar Skip to footer

Dynamically Select Dataframe Columns For Groupby In Python

I have a pandas dataframe named Incoming_Tags I can do groupby on the dataframe by mentioning the column names as input to groupby: Example: Incoming_Tags.groupby([ 'Domain','Tag_

Solution 1:

If want aggregate all numeric columns, non numeric are excluded by default:

defgroup_by(df,myList= [],*args):
    return df.groupby(myList).mean()

Or with c list of columns for specify columns for aggregating:

defgroup_by(df,myList= [],*args): 
    c = ['char_cnt','line_cnt','digit_cnt','sp_chr_cnt', 'word_cnt']
    return df.groupby(myList)[c].mean()

Post a Comment for "Dynamically Select Dataframe Columns For Groupby In Python"