nodes: 0, module; 1, function_definition; 2, function_name:multi_index_insert_row; 3, parameters; 4, block; 5, identifier:df; 6, identifier:index_row; 7, identifier:values_row; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, comment:# The df wasn't sorted or the row has to be put in the middle somewhere; 14, return_statement; 15, comment:""" Return a new dataframe with a row inserted for a multi-index dataframe.
          This will sort the rows according to the ordered multi-index levels.
      """; 16, assignment; 17, assignment; 18, assignment; 19, boolean_operator; 20, comment:# We've just appended a row to an already-sorted dataframe; 21, block; 22, call; 23, identifier:row_index; 24, call; 25, identifier:row; 26, call; 27, identifier:df; 28, call; 29, comparison_operator:df.index.lexsort_depth == len(index_row); 30, comparison_operator:df.index[-2] < df.index[-1]; 31, return_statement; 32, attribute; 33, argument_list; 34, attribute; 35, argument_list; 36, attribute; 37, argument_list; 38, attribute; 39, argument_list; 40, attribute; 41, call; 42, subscript; 43, subscript; 44, identifier:df; 45, identifier:df; 46, identifier:sort_index; 47, identifier:pd; 48, identifier:MultiIndex; 49, keyword_argument; 50, keyword_argument; 51, identifier:pd; 52, identifier:DataFrame; 53, identifier:values_row; 54, keyword_argument; 55, keyword_argument; 56, identifier:pd; 57, identifier:concat; 58, tuple; 59, attribute; 60, identifier:lexsort_depth; 61, identifier:len; 62, argument_list; 63, attribute; 64, unary_operator; 65, attribute; 66, unary_operator; 67, identifier:levels; 68, list_comprehension; 69, identifier:labels; 70, list_comprehension; 71, identifier:index; 72, identifier:row_index; 73, identifier:columns; 74, attribute; 75, identifier:df; 76, identifier:row; 77, identifier:df; 78, identifier:index; 79, identifier:index_row; 80, identifier:df; 81, identifier:index; 82, integer:2; 83, identifier:df; 84, identifier:index; 85, integer:1; 86, list; 87, for_in_clause; 88, list; 89, for_in_clause; 90, identifier:df; 91, identifier:columns; 92, identifier:i; 93, identifier:i; 94, identifier:index_row; 95, integer:0; 96, identifier:i; 97, identifier:index_row
code: def multi_index_insert_row(df, index_row, values_row):
      """ Return a new dataframe with a row inserted for a multi-index dataframe.
          This will sort the rows according to the ordered multi-index levels.
      """
      row_index = pd.MultiIndex(levels=[[i] for i in index_row],
                                labels=[[0] for i in index_row])
      row = pd.DataFrame(values_row, index=row_index, columns=df.columns)
      df = pd.concat((df, row))
      if df.index.lexsort_depth == len(index_row) and df.index[-2] < df.index[-1]:
          # We've just appended a row to an already-sorted dataframe
          return df
      # The df wasn't sorted or the row has to be put in the middle somewhere
      return df.sort_index()