3

I have 2 dataframes:

df1

       hair    eyes   gender  

joe     br      bl      m
mary    bl      br      f
pete    rd      gr      m

and I want to update df1 with values from df2 IF subject to some values in some additional columns

       hair    eyes   gender  weight  height

joe     bk      gr      m       150     72
mary    bl      br      f       125     55
pete    rd      gr      m       180     68

I want to do this:

df1.update(df2)   #if df2 height is over 70

but not sure how or if it's possible to specify condition.

The output I'd get after the operation is:

       hair    eyes   gender  

joe     bk      gr      m
mary    bl      br      f
pete    rd      gr      m

So only Joe was updated because his height was over 70.

Is there some way to conditionally specify update or is it best to just make another df where df2['height'] > 70?

Thanks in advance

1 Answer 1

2

Just filter df2 before you update:

df1.update(df2[df2['height'] > 70])
Sign up to request clarification or add additional context in comments.

2 Comments

I read the documentation and it wasn't clear that I could do that. But I guess I can! Thanks.
@Windstorm1981, No problem. Consider accepting the answer (and this previous one if it helped).

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.