Skip to content Skip to sidebar Skip to footer

Pandas: Replace Values Of One Data Frame With Values Of Another Data Frame Based On Index And Column

I want to merge/join/ two data frame replacing by index. df1 = pd.DataFrame(index=range(5),columns=range(5)) df1 = df1.fillna(0) df1 0 1 2 3 4 0 0 0 0 0 0 1

Solution 1:

Using update notice all method , you need make the index and columns dtype is same , that is why I first convert them to int , since when you create the df2 , the columns is str

df2.columns=df2.columns.astype(int)
df1.update(df2)
df1
Out[961]: 
   012340000.000.01000.000.02002.000.03000.004.04000.000.0

Or reindex_like

df2=df2.reindex_like(df1).fillna(0)
df2
Out[964]: 
     0    1    2    3    4
0  0.0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0  0.0
2  0.0  0.0  2.0  0.0  0.0
3  0.0  0.0  0.0  0.0  4.0
4  0.0  0.0  0.0  0.0  0.0

Post a Comment for "Pandas: Replace Values Of One Data Frame With Values Of Another Data Frame Based On Index And Column"