score:1

Avoiding a one-liner will improve readability, making it a little less confusing:

mask = (csv_pd.setA==1) & (csv_pd.setB==0) & (csv_pd.setC==0)
csv_pd[mask].groupby('D').count()

Another possibility, which happens to be a one-liner, is to use the query method:

csv_pd.query('setA==1 & setB==0 & setC==0').groupby('D').count()

Note also that you can pass a column name to groupby, instead of Series values. Thus, groupby('D') instead of groupby(csv_pd.D).


To count the size of all possible subsets, the powerset recipe and itertools.product would be helpful:

import itertools as IT
import numpy as np
import pandas as pd

def powerset(iterable, reverse=False, rvals=None):
    """powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"""
    s = list(iterable)
    N = len(s)
    if rvals is None:
        rvals = range(N, -1, -1) if reverse else range(N + 1)
    return IT.chain.from_iterable(
        IT.combinations(s, r) for r in rvals)

df = pd.DataFrame(np.random.randint(2, size=(10,4)), columns=list('ABCD'))

print(df)
for cols in powerset(df.columns):
    if not cols: continue
    for vals in IT.product([0,1], repeat=len(cols)):
        mask = np.logical_and.reduce([df[c]==v for c, v in zip(cols, vals)])
        cond = ' & '.join(['{}={}'.format(c,v) for c, v in zip(cols,vals)])
        n = len(df[mask])
        print('n({}) = {}'.format(cond, n))

yields

n(A=0) = 8
n(A=1) = 2
n(B=0) = 4
n(B=1) = 6
...
n(A=0 & B=0) = 4
n(A=0 & B=1) = 4
n(A=1 & B=0) = 0
...
n(A=1 & B=1 & C=0 & D=0) = 0
n(A=1 & B=1 & C=0 & D=1) = 1
n(A=1 & B=1 & C=1 & D=0) = 0
n(A=1 & B=1 & C=1 & D=1) = 1

Related Query