nodes: 0, module; 1, function_definition; 2, function_name:sort_dataframe_cols; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, comment:# get the group for each column; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, comment:# put names first; 13, try_statement; 14, expression_statement; 15, expression_statement; 16, comment:# remove ungrouped columns; 17, if_statement; 18, comment:# flatten list of columns; 19, for_statement; 20, comment:# add back in ungrouped columns; 21, expression_statement; 22, comment:# put name first; 23, try_statement; 24, comment:#; 25, expression_statement; 26, return_statement; 27, comment:"""
          Sort self.df so that self.name is the first column,
          and the rest of the columns are sorted by group.
          """; 28, assignment; 29, assignment; 30, assignment; 31, assignment; 32, block; 33, except_clause; 34, call; 35, assignment; 36, comparison_operator:'' in sorted_cols; 37, block; 38, identifier:k; 39, call; 40, block; 41, call; 42, block; 43, except_clause; 44, assignment; 45, attribute; 46, identifier:cols; 47, attribute; 48, identifier:groups; 49, call; 50, identifier:sorted_cols; 51, call; 52, identifier:ordered_cols; 53, list; 54, expression_statement; 55, identifier:KeyError; 56, block; 57, attribute; 58, argument_list; 59, identifier:no_group; 60, list; 61, string; 62, identifier:sorted_cols; 63, expression_statement; 64, identifier:sorted; 65, argument_list; 66, expression_statement; 67, attribute; 68, argument_list; 69, if_statement; 70, identifier:AttributeError; 71, block; 72, attribute; 73, subscript; 74, identifier:self; 75, identifier:df; 76, attribute; 77, identifier:columns; 78, identifier:list; 79, argument_list; 80, attribute; 81, argument_list; 82, assignment; 83, expression_statement; 84, identifier:ordered_cols; 85, identifier:extend; 86, call; 87, assignment; 88, identifier:sorted_cols; 89, call; 90, identifier:ordered_cols; 91, identifier:extend; 92, identifier:no_group; 93, comparison_operator:self.name in ordered_cols; 94, block; 95, pass_statement; 96, identifier:self; 97, identifier:df; 98, attribute; 99, identifier:ordered_cols; 100, identifier:self; 101, identifier:df; 102, call; 103, identifier:cols; 104, identifier:groupby; 105, identifier:groups; 106, identifier:names; 107, call; 108, assignment; 109, identifier:list; 110, argument_list; 111, identifier:no_group; 112, call; 113, attribute; 114, argument_list; 115, attribute; 116, identifier:ordered_cols; 117, expression_statement; 118, expression_statement; 119, identifier:self; 120, identifier:df; 121, identifier:map; 122, argument_list; 123, attribute; 124, argument_list; 125, identifier:names; 126, list; 127, identifier:names; 128, attribute; 129, argument_list; 130, identifier:ordered_cols; 131, identifier:extend; 132, call; 133, identifier:self; 134, identifier:name; 135, call; 136, assignment; 137, lambda; 138, identifier:cols; 139, identifier:sorted_cols; 140, identifier:pop; 141, string; 142, identifier:sorted_cols; 143, identifier:pop; 144, string; 145, identifier:sorted; 146, argument_list; 147, attribute; 148, argument_list; 149, subscript; 150, list; 151, lambda_parameters; 152, call; 153, string_content:Names; 154, subscript; 155, identifier:ordered_cols; 156, identifier:remove; 157, attribute; 158, identifier:ordered_cols; 159, slice; 160, attribute; 161, identifier:x; 162, attribute; 163, argument_list; 164, identifier:sorted_cols; 165, identifier:k; 166, identifier:self; 167, identifier:name; 168, integer:0; 169, identifier:self; 170, identifier:name; 171, attribute; 172, identifier:get_group_for_col; 173, attribute; 174, identifier:x; 175, identifier:self; 176, identifier:data_model; 177, identifier:self; 178, identifier:dtype
code: def sort_dataframe_cols(self):
          """
          Sort self.df so that self.name is the first column,
          and the rest of the columns are sorted by group.
          """
          # get the group for each column
          cols = self.df.columns
          groups = list(map(lambda x: self.data_model.get_group_for_col(self.dtype, x), cols))
          sorted_cols = cols.groupby(groups)
          ordered_cols = []
          # put names first
          try:
              names = sorted_cols.pop('Names')
          except KeyError:
              names = []
          ordered_cols.extend(list(names))
          no_group = []
          # remove ungrouped columns
          if '' in sorted_cols:
              no_group = sorted_cols.pop('')
          # flatten list of columns
          for k in sorted(sorted_cols):
              ordered_cols.extend(sorted(sorted_cols[k]))
          # add back in ungrouped columns
          ordered_cols.extend(no_group)
          # put name first
          try:
              if self.name in ordered_cols:
                  ordered_cols.remove(self.name)
                  ordered_cols[:0] = [self.name]
          except AttributeError:
              pass
          #
          self.df = self.df[ordered_cols]
          return self.df