Osca
Osca

Reputation: 1694

Naming pandas columns with variables

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.

Upvotes: 1

Views: 45

Answers (1)

BENY
BENY

Reputation: 323226

Just do

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

Upvotes: 3

Related Questions