Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can Pandas GroupBy split into just 2 bins?

Imagine I have this table:

Col-1 | Col-2
A     |   2
A     |   3
B     |   1
B     |   4
C     |   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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

One solution is to make pd.Categorical from the Column 1 -> with two categories A for string A and Other for other strings. Then group by this categorical:

tmp = (
    pd.Categorical(df["Col1"], categories=["A"]).add_categories("Other").fillna("Other")
)

out = df.groupby(tmp, observed=False)["Col2"].sum()
print(out)

Prints:

A         5
Other    12
Name: Col2, dtype: int64

Another solution, group by boolean mask:

out = (
    df.groupby(df["Col1"].eq("A"))["Col2"]
    .sum()
    .rename(index={True: "A", False: "Other"})
)
print(out)

Prints:

Col1
Other    12
A         5
Name: Col2, dtype: int64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading