Question:
I have a data frame as such:ID | Object_1 | Object_2 | Object_3 |
---|---|---|---|
123 | Yes | No | No |
123 | No | Yes | No |
1234 | No | Yes | No |
I want to group by the ID column though the values for Object_1, Object_2 and Object_3 may be different. If the value ‘Yes’ exists, I would like that remain in the final grouped dataframe.
Desired output would be a dataframe with the following values:
ID | Object_1 | Object_2 | Object_3 |
---|---|---|---|
123 | Yes | Yes | No |
1234 | No | Yes | No |
Answer:
You can take advantage of the fact thatYes
is lexicographically sorted after No
:more robust/generic way
You can use an ordered Categorical type to handle any values, even more than two (e.g, No/Maybe/Yes):
If you have better answer, please add a comment about this, thank you!