emax
emax

Reputation: 7255

Python: how multiply columns with each others in pandas?

let say to have a dataframe df like the following:

df
   A   B   C
0  2   3   1
1  1   0   1
2  0   2   1
3  1   2   2

I would like to add columns as multiplication of the columns with each others and have something like

df
   A   B   C   AB  AC  BC
0  2   3   1   6   2   3 
1  1   0   1   0   1   0
2  0   2   1   0   0   2
3  1   2   2   2   2   4

Upvotes: 1

Views: 83

Answers (4)

jezrael
jezrael

Reputation: 863741

Use DataFrame.reindex by MultiIndex.from_tuples and combinations, multiple by DataFrame.mul and last join by DataFrame.join to original:

from  itertools import combinations

c = pd.MultiIndex.from_tuples(combinations(df.columns, 2))
df1 = df.reindex(c, axis=1, level=0).mul(df.reindex(c, axis=1, level=1))
df1.columns = df1.columns.map(''.join)

df = df.join(df1)
print (df)
   A  B  C  AB  AC  BC
0  2  3  1   6   2   3
1  1  0  1   0   1   0
2  0  2  1   0   0   2
3  1  2  2   2   2   4

Upvotes: 0

Ouyang Ze
Ouyang Ze

Reputation: 500

I think the simplest answer is....

from itertools import combinations
df = df.assign(**{(k1+k2): df[k1]*df[k2] for k1,k2 in combinations(df.columns,2)})

Upvotes: 1

yatu
yatu

Reputation: 88305

Here's one approach is to get the column name combinations using itertools.combinations, and to take their product in a list comprehension:

from itertools import combinations
combs = list(map(list,list(combinations(df.columns.tolist(), 2))))
# [['A', 'B'], ['A', 'C'], ['B', 'C']]
new_cols = pd.concat([df[c].prod(1) for c in combs], axis=1)
new_cols.columns = [''.join(i) for i in combs]
df.assign(**new_cols)

   A  B  C  AB  AC  BC
0  2  3  1   6   2   3
1  1  0  1   0   1   0
2  0  2  1   0   0   2
3  1  2  2   2   2   4

Upvotes: 2

MEdwin
MEdwin

Reputation: 2960

Here I have used the basic column operation for simplicity. see if it works for you:

import pandas as pd
df = pd.DataFrame({
    'A':[2,1,0,1],
    'B':[3,0,2,2],
    'C':[1,1,1,2]
})
df['AB']=df['A']*df['B']
df['AC']=df['A']*df['C']
df['BC']=df['B']*df['C']
df

output

A   B   C   AB  AC  BC
0   2   3   1   6   2   3
1   1   0   1   0   1   0
2   0   2   1   0   0   2
3   1   2   2   2   2   4

Upvotes: 1

Related Questions