Home > Back-end >  To find all possible combinations and sum in pandas dataframe
To find all possible combinations and sum in pandas dataframe

Time:06-09

Input: Input dataframe as mentioned below:

enter image description here

Output: All possible combinations to find sum

enter image description here

CodePudding user response:

You can use itertools.combinations and compute sum of values another column like below:

from itertools import combinations

res = [] 
for i in range(2, df['Sno'].nunique() 1):
    for tpl in combinations(df['Sno'], i):
        res.append([tpl,df.loc[df['Sno'].isin(tpl), 'No'].sum()]

# One line:
# res = [[tpl,df.loc[df['Sno'].isin(tpl), 'No'].sum()] for i in range(2, df['Sno'].nunique() 1) for tpl in combinations(df['Sno'], i) ]

Output:

>>> pd.DataFrame(res, columns=['Combinations', 'Sum'])

    Combinations   Sum
0   (A, B)         3
1   (A, C)         4
2   (B, C)         5
3   (A, B, C)      6

Explanation:

>>> list(combinations(['A','B','C'], 2))
[('A', 'B'), ('A', 'C'), ('B', 'C')]

>>> df.loc[df['Sno'].isin(('A','B')), 'No']
0    1
1    2
Name: No, dtype: int64

>>> df.loc[df['Sno'].isin(('A','B')), 'No'].sum()
3

CodePudding user response:

You can make use of combinations from itertools:

from itertools import combinations
e = [[','.join(j),df.No[np.in1d(df.Sno, j)].sum()] for i in df.index   1 for j in combinations(df.Sno,i)]

pd.DataFrame(e)
 
       0  1
0      A  1
1      B  2
2      C  3
3    A,B  3
4    A,C  4
5    B,C  5
6  A,B,C  6
  • Related