GihanDB
GihanDB

Reputation: 631

How to multiply 2 columns in 2 pandas data frames by each other

I have 2 data frames like this

    DF1:    Col1 Col2
    Row1    A   30
    Row2    B   40

    DF2:  Col1 Col2
    Row1    A   10
    Row2    B   5

Now i want to multiply DF1.Col2 and DF2.Col2 and get an output like this

    Out:   Col1 COl3
    Row1    A   300
    Row2    B   200

How can I do this. I try bellow code but its not working

DF1[:,'Col3'] = pd.Series(DF1.Col2.astype(int)*DF2.Col2.astype(int),index=DF1.index)

This direct to bellow error: TypeError: unhashable type

Upvotes: 1

Views: 1383

Answers (1)

jezrael
jezrael

Reputation: 863711

I think you need:

DF1['Col3'] = DF1.Col2.astype(int)*DF2.Col2.astype(int)
print (DF1)
     Col1  Col2  Col3
Row1    A    30   300
Row2    B    40   200

Another solution with mul:

DF1['Col3'] = DF1.Col2.astype(int).mul(DF2.Col2.astype(int))
print (DF1)
     Col1  Col2  Col3
Row1    A    30   300
Row2    B    40   200

Upvotes: 3

Related Questions