Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All possible combinations of columns in dataframe -pandas/python

I'm trying to take one dataframe and create another, with all possible combinations of the columns and the difference between the corresponding values, i.e on 11-apr column AB should be (B-A)= 0 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:

desired result

I have come across the below post, but have not been able to transpose this to work for columns.

Aggregate all dataframe row pair combinations using pandas

like image 643
S.Peters Avatar asked Apr 11 '17 13:04

S.Peters


People also ask

Which are the 3 main ways of combining DataFrames together?

Joining two DataFrames can be done in multiple ways (left, right, and inner) depending on what data must be in the final DataFrame.

How do I get two column combinations in pandas?

By use + operator simply you can combine/merge two or multiple text/string columns in pandas DataFrame. Note that when you apply + operator on numeric columns it actually does addition instead of concatenation.

What is DF unstack?

DataFrame - unstack() functionPivot a level of the (necessarily hierarchical) index labels, returning a DataFrame having a new level of column labels whose inner-most level consists of the pivoted index labels.


1 Answers

You can use:

from itertools import combinations
df = df.set_index('Dt')

cc = list(combinations(df.columns,2))
df = pd.concat([df[c[1]].sub(df[c[0]]) for c in cc], axis=1, keys=cc)
df.columns = df.columns.map(''.join)
print (df)
        AB  AC  AD  BC  BD  CD
Dt                            
11-apr   0   0   0   0   0   0
10-apr   1  -1   0  -2  -1   1
like image 144
jezrael Avatar answered Oct 05 '22 23:10

jezrael