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()
def multi_index_insert_row ( df , index_row , values_row ) : 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 ] : return df return df . sort_index ( )
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.