Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Can Pandas GroupBy split into just 2 bins?

$
0
0

Imagine I have this table:

Col-1 | Col-2A     |   2A     |   3B     |   1B     |   4C     |   7

Groupby on Col-1 with a sum aggregation on Col-2 will sum A to 5, B to 5, and C to 7.

What I want to know is if there is a baked in feature that allows aggregation on a target value in a column and then groups all other entries into another bin. For example, if I wanted to groupby on Col-1 targeting A and grouping all other entries into a label named other, I would end up with A as 5 and Other as 12.

Does that make sense? I know I could do some filtering sorcery and merging datasets back together, but figured there had to be a cleaner, more Pythonic way I am missing.

I have tried going through the documentation, but nothing jumped out at me.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>