Search code examples
pythonpandascombinationsmultiplication

Pandas Multiply multiple columns in loop


I have a dataframe df with 4 columns A, B, C & D.

I want to multiple every combination of these columns.

So far I have;

columns=[A,B,C,D]

a= combinations(columns)

for i in a:
    df[outname]=df[a].multiply()

but obviously this isn't correct.

Can anyone see a good way?

Output:

   A   B   C   D   AB   AC   AD   BC   ABC    and so on
0
1
2
3
4
6
7

Solution

  • Use function from this for find all combinations and in list comprehension create all product of values:

    df = pd.DataFrame({
             'A':[5,3,6,9,2,4],
             'B':[4,5,4,5,5,4],
             'C':[7,8,9,4,2,3],
             'D':[1,3,5,7,1,0],
    
    })
    
    from itertools import chain, combinations
    def all_subsets(ss):
        return chain(*map(lambda x: combinations(ss, x), range(1, len(ss)+1)))
    
    #get all combination
    tups = list(all_subsets(df.columns))
    #for each combination multiple values
    df1 = pd.concat([df.loc[:,c].product(axis=1) for c in tups], axis=1)
    #set new columns by join list of tuples tups
    df1.columns = [''.join(x) for x in tups]
    print (df1)
       A  B  C  D  AB  AC  AD  BC  BD  CD  ABC  ABD  ACD  BCD  ABCD
    0  5  4  7  1  20  35   5  28   4   7  140   20   35   28   140
    1  3  5  8  3  15  24   9  40  15  24  120   45   72  120   360
    2  6  4  9  5  24  54  30  36  20  45  216  120  270  180  1080
    3  9  5  4  7  45  36  63  20  35  28  180  315  252  140  1260
    4  2  5  2  1  10   4   2  10   5   2   20   10    4   10    20
    5  4  4  3  0  16  12   0  12   0   0   48    0    0    0     0