Turn correlation dataframe into list

Viewed 31

I have a correlation dataframe, and I'm trying to turn it into different lists:

        A          B            C 
A   1.000000    0.932159    -0.976221

B   0.932159    1.000000    -0.831509

C   -0.976221   -0.831509   1.000000

The output I need is:

[A, B, 0.932159]
[A, C, -0.976221]
[B, A, 0.932159]
[B, C, -0.831509]
[C, A, -0.976221]
[C, B, -0.831509]

I have tried converting the dataframe into list, but I don't get what I need. Thanks

3 Answers

Stack the dataframe, reset the index, then exclude the rows which have identical 1st and 2nd column values, then create the list out of it:

out=df.stack().reset_index()
out=out[out.iloc[:,0].ne(out.iloc[:,1])].values.tolist()

OUTPUT

[['A', 'B', 0.932159],
 ['A', 'C', -0.976221],
 ['B', 'A', 0.932159],
 ['B', 'C', -0.831509],
 ['C', 'A', -0.976221],
 ['C', 'B', -0.831509]]

The simplest way I can think of for a tiny DataFrame like this is with a list comprehension:


corr

          A         B         C
A  1.000000  0.932159 -0.976221
B  0.932159  1.000000 -0.831509
C -0.976221 -0.831509  1.000000


[[row, col, corr[col][row]] for row in corr.index for col in corr if row != col]

[['A', 'B', 0.932159],
 ['A', 'C', -0.976221],
 ['B', 'A', 0.932159],
 ['B', 'C', -0.831509],
 ['C', 'A', -0.976221],
 ['C', 'B', -0.831509]]

The longer-form way may be easier to read for a general audience:

result = []
for row in corr.index:
    for col in corr:
        if row != col:
            result.append([row, col, corr[col][row]])


result

[['A', 'B', 0.932159],
 ['A', 'C', -0.976221],
 ['B', 'A', 0.932159],
 ['B', 'C', -0.831509],
 ['C', 'A', -0.976221],
 ['C', 'B', -0.831509]]

A simple list comprehension will be enough

from itertools import permutations

elements = df.index
out = [[val[0], val[1], df.loc[val[0], val[1]]] 
        for val in permutations(elements, 2)] 

Output

[['A', 'B', 0.932159],
 ['A', 'C', -0.976221],
 ['B', 'A', 0.932159],
 ['B', 'C', -0.831509],
 ['C', 'A', -0.976221],
 ['C', 'B', -0.831509]]
Related