• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Pandas – Grouping by column value and detecting values

Resolved: Pandas – Grouping by column value and detecting values

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

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 that Yes is lexicographically sorted after No:
Output:

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!

group-by pandas python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: std::regex_replace to replace multiple combinations

26/03/2023

Resolved: How can I copy files using the ansible.builtin.copy module and avoid conflicting file names?

26/03/2023

Resolved: Reshape tensors of unknown shape with tf.function

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.