Home > front end >  Looping through pairwise combinations of columns
Looping through pairwise combinations of columns

Time:12-04

Here is an example dataframe:

d = {'Gender': [1,1,0,1,0], 'Employed': [1,0,0,1,1], 'Name':['Alan', 'Joe', 'Sam', 'Amy', 
'Chloe']}
d=pd.DataFrame(d)

I wanted to get all pairwise combinations of the columns so I could pass them into a fisher's exact test:

from itertools import combinations
pairwise_combinations=list(combination(d.columns, 2))
pairwise_combinations=[', '.join(map(str, x)) for x in pairwise_combinations]

This gives me the pairs in the form:

Gender, Employed

I want to be able to say

for i in pairwise_combinations:
    data=d[[i]]

At the moment this gives an error because it is still in the wrong form. I need quotes around the column names. How can I do this?

CodePudding user response:

If I understand well what you want is this :

from itertools import combinations
pairwise_combinations = list(combinations(d.columns, 2))

for i in pairwise_combinations:
    data = d.loc[:, i]

CodePudding user response:

You can quite easily loop through the pairs by using the zip() function to attach the columns to each other. You don't need to make it a pd dataframe, either:

>>> for i in zip(d["Gender"],d["Employed"]): print(i)

(1, 1)
(1, 0)
(0, 0)
(1, 1)
(0, 1)

I don't know what exactly you need as an input for the Fisher's test but that would be an easy solution to get the value pairs in a loop.

  • Related