hkml
hkml

Reputation: 339

I need to insert a row at nth index that will take summation of all rows that are underneath it

I have a dataframe with 30 rows. I need to insert a row at the 10th index, give it a name and then have all the cells in it, be the summation of all cells that are underneath it. It will represent a total of the lower performing parts.

pd.DataFrame(np.insert(df.values, 0,)

I would like to name give it an index name, and keep all the other data below. simply and insertion and the summation of all rows underneath it.

Upvotes: 1

Views: 129

Answers (1)

jezrael
jezrael

Reputation: 862731

I pandas exist DataFrame.insert, but working only for columns, so is necessary something more complicated:

df = pd.DataFrame({
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
})

Solutions for include rows with index idx for sum:

idx = 3
df1 = df.iloc[:idx]
df2 = df.iloc[idx:]

df = pd.concat([df1, df2.sum().to_frame('new').T, df2])
print (df)
      B  C
0     4  7
1     5  8
2     4  9
new  14  9
3     5  4
4     5  2
5     4  3

Or:

idx = 3
df.loc[idx + .5] = df.iloc[idx:].sum()
df = df.sort_index().rename({idx + .5:'new'})
print (df)
        B    C
0.0   4.0  7.0
1.0   5.0  8.0
2.0   4.0  9.0
3.0   5.0  4.0
new  14.0  9.0
4.0   5.0  2.0
5.0   4.0  3.0

Solutions for exclude row with idx for sum:

idx = 3
df1 = df.iloc[:idx+1]
df2 = df.iloc[idx+1:]

df = pd.concat([df1, df2.sum().to_frame('new').T, df2])
print (df)
     B  C
0    4  7
1    5  8
2    4  9
3    5  4
new  9  5
4    5  2
5    4  3

idx = 3
df.loc[idx + .5] = df.iloc[idx + 1:].sum()
df = df.sort_index().rename({idx + .5:'new'})
print (df)
       B    C
0.0  4.0  7.0
1.0  5.0  8.0
2.0  4.0  9.0
3.0  5.0  4.0
new  9.0  5.0
4.0  5.0  2.0
5.0  4.0  3.0

If all columns are numeric, is possible use also np.insert:

idx = 3

arr = df.to_numpy()
s = arr[idx:].sum(axis=0)[None, :]
np.insert(arr, 1, s, 0)  

df = pd.DataFrame(arr, columns=df.columns).rename({idx:'new'})
print (df)
     B  C
0    4  7
1    5  8
2    4  9
new  5  4
4    5  2
5    4  3

Upvotes: 2

Related Questions