Pandas- Merging Two Dataframe By Sum The Values Of Columns And Index
I want to merge two datasets by indexes and columns. I want to merge entire dataset df1 = pd.DataFrame([[1, 0, 0], [0, 2, 0], [0, 0, 3]],columns=[1, 2, 3]) df1 1 2 3 0 1
Solution 1:
You can use :df1.add(df2, fill_value=0)
. It will add df2
into df1
also it will replace NAN
value with 0
.
>>> import numpy as np
>>> import pandas as pd
>>> df2 = pd.DataFrame([(10,9),(8,4),(7,np.nan)], columns=['a','b'])
>>> df1 = pd.DataFrame([(1,2),(3,4),(5,6)], columns=['a','b'])
>>> df1.add(df2, fill_value=0)
a b
0 11 11.0
1 11 8.0
2 12 6.0
Post a Comment for "Pandas- Merging Two Dataframe By Sum The Values Of Columns And Index"