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?
>Solution :
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]