I have a dataframe containing a set of columns all of which are booleans. I would like to get a unique count of another column (an ID column) for each of these columns (and respective boolean values)
ex:
data = [
["ABC", True, False],
["DEF", False, False],
["GHI", False, True],
]
df = pd.DataFrame(data, columns=["ID", "CONTAINS_Y", "CONTAINS_X"])
df.groupby("CONTAINS_Y")["ID"].nunique().reset_index()
yields:
CONTAINS_Y ID
False 2
True 1
I would like a command that yields:
CONTAINS_Y ID
False 2
True 1
CONTAINS_X ID
False 2
True 1
Is there a way to group all boolean columns to get unique counts? Or does it have to be done individually?
There are other similar questions (Pandas count(distinct) equivalent) but the solutions haven't worked for what I am trying to achieve.