-1

I am working to reduce the rows in my dataframe based on the values in the first rows. If the value in the first column of a row matches the value in the second row of the first, I want to add the data in the residual columns.

My dataframe looks like this:

position length height
1 5.5 0.0
1 0.0 2.1
2 4.1 0.0
2 0.0 3.5

My desired output is this:

position length height
1 5.5 2.1
2 4.1 3.5
Zoe
  • 27,060
  • 21
  • 118
  • 148
Christina
  • 7
  • 1

1 Answers1

0

Try .groupby + .sum():

x = df.groupby("position", as_index=False).sum()
print(x)

Prints:

   position  length  height
0         1     5.5     2.1
1         2     4.1     3.5
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91