Conditional multi row matching
I have two pandas DataFrames
a = DataFrame({"A": ["foo", "goo", "doo", "boo"],
"B": [1, 2, 3, 4],
"C": [4, 3, 1, 2]
})
b = DataFrame({"A": ["foo", "goo", "doo", "boo"],
"E": [1, 2, 3, 4],
"D": [0, 1, 0, 0]
})
and I want to match them.
The complicated part is:
I want to create a new column "NEW" in DataFrame "a" containing the value
of "D" from DataFrame "b" only if values from columns "A" AND ("B" OR "C")
are the same.
So the result of the example would be
A B C NEW
0 foo 1 4 0
1 goo 2 3 1
2 doo 3 1 0
3 boo 4 2 1
I have asked this question dealing with the same problem in R some time
ago (data was organized in lists, so a bit different) but now I fail using
the same approach in python.
Original/related question: Conditional matching of two lists with
multi-column data.frames
Thanks
No comments:
Post a Comment