Search code examples
pandasappenddataframerowsreindex

Pandas DataFrame: append give index by name to end of df


I have df which looks like:

df_table
Name       exp1
bnb_q8a12    41
bnb_q8a8     36
bnb_q8a2     30
_xexp0       20
_xexp73      16
bnb_q8a7     10
_xexp216     10
bnb_q8a3     10

I would like the df to look like this, with all index which are named "Net: _xexp.." appended at the end of the df.

df_table
Name       exp1
bnb_q8a12         41
bnb_q8a8          36
bnb_q8a2          30
bnb_q8a7          10
bnb_q8a3          10
Net: _xexp0       20
Net: _xexp73      16
Net: _xexp216     10

I am current appending the given index like this but Im sure there is a better way of doing this?

res = df_table.index.tolist()
nets = []
for i in df_table.index:
    if 'Net: ' in i:
        nets.append(res.pop(res.index(i)))

res.extend(nets)
df_table = df_table.reindex(res)

Solution

  • Use the vectorised str.contains method to mask the df for the rows that contain the string and prepend the str. As it's currently your index, it's probably simpler to reset the index to restore it as a column, then filter the rows and prepend the text, use concat on the filtered rows to reorder them and then set the index back again:

    In [64]:
    
    df = df.reset_index()
    df.loc[df.Name.str.contains('_xexp'), 'Name'] = 'Net: ' + df.Name
    df
    Out[64]:
                Name  exp1
    0      bnb_q8a12    41
    1       bnb_q8a8    36
    2       bnb_q8a2    30
    3    Net: _xexp0    20
    4   Net: _xexp73    16
    5       bnb_q8a7    10
    6  Net: _xexp216    10
    7       bnb_q8a3    10
    
    In [65]:
    
    df = pd.concat([df[~df.Name.str.contains('_xexp')], df[df.Name.str.contains('_xexp')]])
    df
    Out[65]:
                Name  exp1
    0      bnb_q8a12    41
    1       bnb_q8a8    36
    2       bnb_q8a2    30
    5       bnb_q8a7    10
    7       bnb_q8a3    10
    3    Net: _xexp0    20
    4   Net: _xexp73    16
    6  Net: _xexp216    10
    In [66]:
    
    df = df.set_index('Name')
    df
    Out[66]:
                   exp1
    Name               
    bnb_q8a12        41
    bnb_q8a8         36
    bnb_q8a2         30
    bnb_q8a7         10
    bnb_q8a3         10
    Net: _xexp0      20
    Net: _xexp73     16
    Net: _xexp216    10