Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform mathematical operation on a matching columns from 2 data frames: Python Pandas

I have these two data frames with different row indices but some columns are same. What I want to do is to a get a data frame that sums the numbers of the two data frames with same column names

df1 = pd.DataFrame([(1,2,3),(3,4,5),(5,6,7)], columns=['a','b','d'], index = ['A', 'B','C','D'])
df1
   a  b  d
A  1  2  3
B  3  4  5
C  5  6  7

df2 = pd.DataFrame([(10,20,30)], columns=['a','b','c'])

df2
    a   b   c
0  10  20  30

Output dataframe:

    a   b  d
A  11  22  3
B  13  24  5
C  15  16  7

Whats the best way to do this? .add() doesn't seem to work with data frames with different indices.

like image 652
cryp Avatar asked Oct 14 '25 04:10

cryp


2 Answers

This one-liner does the trick:

In [30]: df1 + df2.ix[0].reindex(df1.columns).fillna(0)
Out[30]:
    a   b  d
A  11  22  3
B  13  24  5
C  15  26  7
like image 133
S Anand Avatar answered Oct 16 '25 16:10

S Anand


Here's one way to do it.

Extract common columns on which you want to add from df1 and df2.

In [153]: col1 = df1.columns

In [154]: col2 = df2.columns

In [155]: cols = list(set(col1) & set(col2))

In [156]: cols
Out[156]: ['a', 'b']

And, now add the values

In [157]: dff = df1

In [158]: dff[cols] = df1[cols].values+df2[cols].values

In [159]: dff
Out[159]:
    a   b  d
A  11  22  3
B  13  24  5
C  15  26  7
like image 33
Zero Avatar answered Oct 16 '25 17:10

Zero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!