简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM