Skip to content Skip to sidebar Skip to footer

Inserting New Rows In Pandas Data Frame At Specific Indices

I have a following data frame df with two columns 'identifier', 'values' and 'subid': identifier values subid 0 1 101 1 1 1 102 1

Solution 1:

Preserving the index order is the tricky part. I'm not sure this is the most efficient way to do this, but it should work.

x = [2,8,12]
rows = []
cur = {}

for i in df.index:
    if i in x:
        cur['index'] = i
        cur['identifier'] = df.iloc[i].identifier
        cur['values'] = df.iloc[i]['values']
        cur['subid'] = df.iloc[i].subid - 1
        rows.append(cur)
        cur = {}

Then, iterate through the new rows list, and perform an incremental concat, inserting each new row into the correct spot.

offset=0; #tracks the number ofrows already inserted to ensure rowsare inserted in the correct position

for d inrows:
    df = pd.concat([df.head(d['index'] +offset), pd.DataFrame([d]), df.tail(len(df) - (d['index']+offset))])
    offset+=1


df.reset_index(inplace=True)
df.drop('index', axis=1, inplace=True)
df

    level_0 identifier  subid   values0011101111110220111033212103431210454121056523106762310787231089023109108241091192411012103511113113511214035113151236113

Solution 2:

subtract where the prior row is different than the current row

# edit in placedf['values'] -= df.identifier.ne(df.identifier.shift().bfill())
df

    identifier  values
0            1     101
1            1     102
2            1     103
3            1     104
4            1     105
5            2     105
6            2     107
7            2     108
8            2     109
9            2     110
10           3     110
11           3     112
12           3     113

Or

# new dataframe
df.assign(values=df['values'] - df.identifier.ne(df.identifier.shift().bfill()))

    identifier  values01101111022110331104411055210562107721088210992110103110113112123113

Post a Comment for "Inserting New Rows In Pandas Data Frame At Specific Indices"