Reputation: 163
I have 2 dataframes
df1
a b c
1 2 3
2 4 5
3 6 7
and
df2
a b c
1 3 4
3 1 8
I want output to be
df3
a b c
1 5 7
2 4 5
3 7 15
I tried df1.add(df2,axis='c')
but not getting exact output.
referring this link http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.add.html
Upvotes: 2
Views: 241
Reputation: 863166
You need set_index
by column a
in both df
with add
and parameter fill_value=0
.
Last if necessary convert values to int
and reset_index
:
df = df1.set_index('a').add(df2.set_index('a'),fill_value=0).astype(int).reset_index()
print (df)
a b c
0 1 5 7
1 2 4 5
2 3 7 15
For removing not common rows omit fill_value
and add dropna
if no NaN
in both DataFrames
df = df1.set_index('a').add(df2.set_index('a')).dropna().astype(int).reset_index()
print (df)
a b c
0 1 5 7
1 3 7 15
Upvotes: 4
Reputation: 11477
Maybe you can try this:
>>> import pandas as pd
>>>
>>> df1 = pd.DataFrame([(1, 2, 3), (2, 4, 5), (3, 6, 7)], columns=['a', 'b', 'c'])
>>>
>>> df2 = pd.DataFrame([(1, 3, 4), (3, 1, 8)], columns=['a', 'b', 'c'])
>>>
>>> df1.index = df1['a']
>>> del df1['a']
>>>
>>> df2.index = df2['a']
>>> del df2['a']
>>>
>>> df1.add(df2, fill_value=0)
b c
a
1 5.0 7.0
2 4.0 5.0
3 7.0 15.0
Or use set_index()
method:
>>> df1.set_index('a').add(df2.set_index('a'), fill_value=0)
b c
a
1 5.0 7.0
2 4.0 5.0
3 7.0 15.0
Upvotes: 1