TChi
TChi

Reputation: 383

Python Multiplying many columns in a Dataframe on many columns

Thanks for any help. I am trying to multiply several columns with several other columns to create, in this example, 6 new columns (AC, AD, AE, BC, BD, BE ). As you can see there is a datetype index and an id column. This table is only an example of a much larger Dataframe.

            id      A           B           C           D           E         
2017-12     93426   0.687377    -4.000753   -3.191796   0.235393    0.0071  
2017-12     93428   0.240590    -4.000753   -3.191796   0.235393    0.0071  
2017-12     93429   0.052937    -4.000753   -3.191796   0.235393    0.0071  
2017-12     93434   0.910938    -4.000753   -3.191796   0.235393    0.0071  
2017-12     93436   0.137670    -4.000753   -3.191796   0.235393    0.0071  
2018-01     93426   3.362003    -2.997135   -2.029331   1.016955    0.011298
2018-01     93428   1.330341    -2.997135   -2.029331   1.016955    0.011298
2018-01     93429   1.579284    -2.997135   -2.029331   1.016955    0.011298    

My attempt:

df[['A','B']].mul(df[['C','D','E']])

>>> TypeError: Cannot compare type 'Period' with type 'str'

Any help is always greatly appreciated!

Upvotes: 1

Views: 51

Answers (2)

Erfan
Erfan

Reputation: 42906

Using itertools.product:

from itertools import product

l1 = ['A', 'B']
l2 = ['C', 'D', 'E']

for c1, c2 in product(l1, l2):
    df[f'{c1}{c2}'] = df[c1].mul(df[c2])


            id         A         B         C         D         E        AC  \
2017-12  93426  0.687377 -4.000753 -3.191796  0.235393  0.007100 -2.193967   
2017-12  93428  0.240590 -4.000753 -3.191796  0.235393  0.007100 -0.767914   
2017-12  93429  0.052937 -4.000753 -3.191796  0.235393  0.007100 -0.168964   
2017-12  93434  0.910938 -4.000753 -3.191796  0.235393  0.007100 -2.907528   
2017-12  93436  0.137670 -4.000753 -3.191796  0.235393  0.007100 -0.439415   
2018-01  93426  3.362003 -2.997135 -2.029331  1.016955  0.011298 -6.822617   
2018-01  93428  1.330341 -2.997135 -2.029331  1.016955  0.011298 -2.699702   
2018-01  93429  1.579284 -2.997135 -2.029331  1.016955  0.011298 -3.204890   

               AD        AE         BC        BD        BE  
2017-12  0.161804  0.004880  12.769587 -0.941749 -0.028405  
2017-12  0.056633  0.001708  12.769587 -0.941749 -0.028405  
2017-12  0.012461  0.000376  12.769587 -0.941749 -0.028405  
2017-12  0.214428  0.006468  12.769587 -0.941749 -0.028405  
2017-12  0.032407  0.000977  12.769587 -0.941749 -0.028405  
2018-01  3.419006  0.037984   6.082179 -3.047951 -0.033862  
2018-01  1.352897  0.015030   6.082179 -3.047951 -0.033862  
2018-01  1.606061  0.017843   6.082179 -3.047951 -0.033862  

Details:

itertools.product gives us the combinations of the two lists, so we iterate through these combinations and create our columns:

list(product(l1, l2))

[('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'C'), ('B', 'D'), ('B', 'E')]

Helpful Edit from OC

I am using a python 3.4 and had to use the .format function

df['{c1}{c2}'.format(c1=c1, c2=c2)]

Upvotes: 2

Quang Hoang
Quang Hoang

Reputation: 150735

Broadcasting is not a bad option:

pd.DataFrame(
    (df[['A','B']].values[:,:,None]
        * df[['C','D','E']].values[:,None,:]
    ).reshape(len(df),-1),
    columns = [f'{x}{y}' for x in 'AB' for y in 'CDE'],
    index = df.index
)

Output:

               AC        AD        AE         BC        BD        BE
2017-12 -2.193967  0.161804  0.004880  12.769587 -0.941749 -0.028405
2017-12 -0.767914  0.056633  0.001708  12.769587 -0.941749 -0.028405
2017-12 -0.168964  0.012461  0.000376  12.769587 -0.941749 -0.028405
2017-12 -2.907528  0.214428  0.006468  12.769587 -0.941749 -0.028405
2017-12 -0.439415  0.032407  0.000977  12.769587 -0.941749 -0.028405
2018-01 -6.822617  3.419006  0.037984   6.082179 -3.047951 -0.033862
2018-01 -2.699702  1.352897  0.015030   6.082179 -3.047951 -0.033862
2018-01 -3.204890  1.606061  0.017843   6.082179 -3.047951 -0.033862

Upvotes: 1

Related Questions