0

I have two dataframes:


df1 :
    A     B     C
0   a0    b0    c0
1   a1    b1    c1
2   a2    b2    c2
3   a3    b3    c3
4   a4    b4    c4

df2 :
    A     B     C
0   a0    b0    c11
1   a1    b1    c5
2   a70   b2    c20
3   a3    b9    c9

In df1, for every row, whenever Column A and Column B values are equal to values in df2, column C should be updated with value from df2.

Output:
    A     B     C
0   a0    b0    c11
1   a1    b1    c5
2   a2    b2    c2
3   a3    b3    c3
4   a4    b4    c4

I tried the following, but it did not work.

df1.set_index(['A', 'B'])
df2.set_index(['A', 'B'])
df1.update(df2)
df1.reset_index()
df2.reset_index()
2
  • what about the last row? u ignore it? Commented Dec 5, 2020 at 17:53
  • 1
    Your solution is right. You need to assign to df1 and df2, when setting the index. From your code above, it seems that you did not. I will delete my answer, because it is not needed. You got the solution right. Commented Dec 5, 2020 at 21:44

1 Answer 1

1
df1["C"][:4] = np.where((df1["A"][:4]==df2["A"])&(df1["B"][:4]==df2["B"]),df2["C"],df1["C"][:4])


    A   B   C
0   a0  b0  c11
1   a1  b1  c5
2   a2  b2  c2
3   a3  b3  c3
4   a4  b4  c4
Sign up to request clarification or add additional context in comments.

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
@DonaldDuck once the OP answers i will be more than happy to explain everything

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.