PekkiPo
PekkiPo

Reputation: 41

Pandas df. Match values of a column from one dataframe with a values from a column from another dataframe

There it seems to be some similar questions and solutions but I really can't apply properly any of them to my problem.

The problem is the following:

I have one df called views

s_id   cookie   product_id  brand
11      1221      1           0
22      12312     1           0
33      231       2           1
44      23123     3           2

The other is purchases

s_id   cookie   product_id   price
11      1221      1           100
22      12312     1           100
33      231       2           200
44      23123     3           300

Session id and cookie play no role here, I'll use them later for other purposes, what I need is basically map product_id in purchases with brand from views, i.e. I want to have purchases dataframe to look like this:

s_id   cookie   product_id   price   brand
11      1221      1           100    0
22      12312     1           100    0
33      231       2           200    1
44      23123     3           300    2

Help me please! Thanks in advance

Upvotes: 0

Views: 420

Answers (1)

Yuca
Yuca

Reputation: 6091

Sounds like pd.merge

dfC = pd.merge(dfA, dfB)

Output

s_id    cookie  product_id  brand   price
0   11  1221    1               0   100
1   22  12312   1               0   100
2   33  231     2               1   200
3   44  23123   3               2   300

Upvotes: 2

Related Questions