user3251889
user3251889

Reputation: 53

Fill the dataframe values from other dataframe in pandas python

I have 2 dataframes df1 and df2. df1 is filled with values and df2 is empty.

df1 and df2, as it can be seen, both dataframes's index and columns will always be same, just difference is df1 doesn't contain duplicate values of columns and indexes but df2 does contain.

How to fill values in df2 from df1, so that it also considers the combination of index and columns?

enter image description here

df1 = pd.DataFrame({'Ind':pd.Series([1,2,3,4]),1:pd.Series([1,0.2,0.2,0.8])
                    ,2:pd.Series([0.2,1,0.2,0.8]),3:pd.Series([0.2,0.2,1,0.8])
                    ,4:pd.Series([0.8,0.8,0.8,1])})
df1 = df1.set_index(['Ind'])

df2 = pd.DataFrame(columns = [1,1,2,2,3,4], index=[1,1,2,2,3,4])

Upvotes: 0

Views: 55

Answers (1)

mcsoini
mcsoini

Reputation: 6642

IIUC, you want to update:

df2.update(df1)
print(df2)

     1    1    2    2    3    4
1  1.0  1.0  0.2  0.2  0.2  0.8
1  1.0  1.0  0.2  0.2  0.2  0.8
2  0.2  0.2  1.0  1.0  0.2  0.8
2  0.2  0.2  1.0  1.0  0.2  0.8
3  0.2  0.2  0.2  0.2  1.0  0.8
4  0.8  0.8  0.8  0.8  0.8  1.0

Upvotes: 2

Related Questions