0

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])

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.