1

I have a list of tuples, which has all possible combinations of a dataframe's columns. I'd like to create new dataframe columns by combining these columns' values.

Example dataframe

d = {'c1':['a', 'b', 'c'], 'c2':['Low', 'Low', 'High'], 'c3':['True', 'True', 'False']}
dd = pd.DataFrame(data=d)

All possible columns combinations with length 2

from itertools import combinations 
com = list(combinations(dd.columns, 2))

It returns[('c1', 'c2'), ('c1', 'c3'), ('c2', 'c3')]

I'd like to create new columns with the above combinations

For example, creating a combined column dd['c1 + c2'] = dd['c1'] + '+' + dd['c2']

    c1  c2      c1 + c2
0   a   Low     a+Low
1   b   Low     b+Low
2   c   High    c+High

The real dataframe has many columns so I would like to automate the process by looping through the tuple list and use variables as dataframe's new column names.

Something like this:

[dd[f'dd[i[0]] + dd[i[1]]'] = dd[i[0]] + '+' + dd[i[1]] for i in com]

where dd[i[0]] = 'c1' and dd[i[1]] = 'c2'

I'm pretty sure I cannot use f-strings in dataframe columns but don't know how to make it work.

1

1 Answer 1

3

Just do

for i in com:
    dd[f'{i[0]} + {i[1]}']= dd[i[0]] + '+' + dd[i[1]] 
    
Sign up to request clarification or add additional context in comments.

1 Comment

The new column name will become "i[0] + i[1]" but I want the column name as "c1 + c2"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.