-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 stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
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
  • 118,151
  • 13
  • 38
  • 75