Home > Software engineering >  Sum rows together starting from specific index in Pandas
Sum rows together starting from specific index in Pandas

Time:06-30

Hello my data frame has 11 rows and I'm trying to make it so the 4th row is the sum of row 4 to 11 sum

I tried to seperate the dataframe in 2, sum the second half ,drop the wanted rows from the first and then merge the two half together with concat but is doesn't seem to work. Here's a sample code:

drop_piv_rank=pivot_rank_famille.drop([4,5,6,7,8,9,10,11], axis=0)
frames_rank=[drop_piv_rank, pivot_rank_famille_grande]
pivot_rank_famille_regroup= pd.concat(frames_rank)
pivot_rank_famille_regroup.head()

Here is the output it gives me : what my code outputs

I'm not an expert in Pandas so I'd really appreciate it if someone could help.

CodePudding user response:

That should do the trick. I believe row 4 means 3 since the index starts at 0

df.loc[3]  = df.loc[10]

CodePudding user response:

df.loc[3] = df.loc[3:10].sum()
  • Related