#NEWLINE# def group_data( data , cols , degree = 3 ) : #NEWLINE# #INDENT# "\n    Create higher-order combinations of categorial features\n    \n    Args:\n        data - a pandas DataFrame\n        cols - a list of column names that will be used for creating higher-order\n               groupings\n        degree - an integer identifying the order of the combinations\n    \n    Returns:\n        a DataFrame with additional colums for each of the higher-order column\n        combination. The names of the new columns are concatenated strings\n        of the column names used in the combination separated by an '_'\n        \n        Eg: 2nd degree combinations of data['col1'] and data['col2'] are located\n            in data['col1_col2']\n    " #NEWLINE# new_data = [ ] #NEWLINE# ( m , n ) = data [ cols ] . shape #NEWLINE# for indices in combinations ( range ( n ) , degree ) : #NEWLINE# #INDENT# group_ids = data . groupby ( list ( data [ cols ] . columns [ list ( indices ) ] ) ) . grouper . group_info [ 0 ] #NEWLINE# new_data . append ( indices ) #NEWLINE# #UNINDENT# data [ '_' . join ( cols ) ] = np . array ( new_data ) . flatten ( ) #NEWLINE# return data