1

I've been working with Python for a short time and I usually find the answers in the web when I have a problem, but I don't find something similar and I feel stuck, so I am going to explain my problem.

In a simplified way, I have a dataframe like this:

df:

ID X Y Z
A 1 0 0
A 0 0 1
A 1 0 0
B 0 1 0
B 1 0 0
B 0 0 1
B 1 0 0

I want to move all the '1s' from the X, Y and Z columns to the row with the first value of their respective ID column.

This would be the desired output:

end_df:

ID X Y Z
A 1 0 1
B 1 1 1

I have thought that I could move the values and then delete all the remaining rows with df.groupby('ID').first(), but I don't know how I could move the values up to their respective rows.

Valderas
  • 45
  • 7

1 Answers1

2

Do you mean by?

>>> df.groupby('ID', as_index=False).max()
  ID  X  Y  Z
0  A  1  0  1
1  B  1  1  1
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114