erogol
erogol

Reputation: 13624

How can I multiply each column with the other columns of the Pandas DataFrame?

Given a pandas dataframe I would like to multiply each column with the other columns one by one and return each new column as a new column to that dataframe. For example

A B C
1 2 3
2 4 4
1 2 5

then

A B C A*B   A*C     B*C
1 2 2  2     3       6
2 4 8  8     8       16
1 2 2  2     5       10

Upvotes: 2

Views: 1136

Answers (2)

Matt
Matt

Reputation: 17649

combinations from itertools does what you're looking for:

import pandas as pd
from itertools import combinations

for c1, c2 in combinations(df.columns, 2):
    df['{0}*{1}'.format(c1,c2)] = df[c1] * df[c2]

df now contains your desired columns:

   A  B  C  A*B  A*C  B*C
0  1  2  1    2    1    2
1  2  4  2    8    4    8
2  3  4  5   12   15   20

If you don't want to keep everything in memory you can calculate the product on the fly:

for c1, c2 in combinations(df.columns, 2):
    s = df[c1] * df[c2]
    # Do whatever is necessary with s
    print c1, c2, s.apply(lambda x: x ** 0.5).mean()

Output:

A B 2.56891410075
A C 2.29099444874
B C 2.90492554737

Upvotes: 1

Dov Grobgeld
Dov Grobgeld

Reputation: 4983

The following is a brute force method, but it should do the job. permutations() generates all the column permutations. The inner sorted() together with set() merges ('A','B') with ('B','A'), etc.

import pandas as pd
import itertools

df = pd.DataFrame([[1,2,1],[2,4,2],[3,4,5]],columns=['A','B','C'])

for c1,c2 in sorted(set([tuple(sorted(s)) for s in itertools.permutations(df.columns,2)])):
  df['{0}x{1}'.format(c1,c2)] = df[c1]*df[c2]

print df

Upvotes: 2

Related Questions