Search code examples
pythonpandaspython-itertools

All possible combinations of 3 columns in a dataframe


This is a follow up to a previous question here

combinations of two columns

I'm trying to take one dataframe and create another, with all possible combinations of 3 columns together and the difference between the corresponding values, i.e on 11-apr column ABC should be (2B -A - C)= 0, then 2*B-A-D = 0 and so on etc.

e.g, starting with

        Dt              A           B           C          D
        11-apr          1           1           1          1
        10-apr          2           3           1          2

how do I get a new frame that looks like this:

enter image description here


Solution

  • I think need:

    cc = list(combinations(df.columns,3))
    
    df = pd.concat([df[c[1]].mul(2).sub(df[c[2]]).sub(df[c[0]]) for c in cc], axis=1, keys=cc)
    df.columns = df.columns.map(''.join)
    print (df)
            ABC  ABD  ACD  BCD
    Dt                        
    11-apr    0    0    0    0
    10-apr    3    2   -2   -3