0

I have a dataframe with combinations between occurencers in two columns:

    A   B
0   a   z
1   a   y
2   b   z
3   c   z
4   c   y
5   c   x

And a matrix in a different dataframe with the values got from the combination of all possible values of A and B:

    x   y   z
a   0.5 0.3 0.2
b   0.3 0.9 0.1
c   0.6 0.8 0.7

I would like to get their interseption in a new column of the main dataframe, searching the pair of values in this table and getting the correspondent interseption from the other dataframe. Example:

    A   B   C
0   a   z   0.2
1   a   y   0.3
2   b   z   0.1
3   c   z   0.7
4   c   y   0.8
5   c   x   0.6

Can anyone please help?

Cookye
  • 23
  • 4

1 Answers1

1

Use DataFrame.join with DataFrame.stack:

df = df1.join(df2.stack().rename('C'), on=['A','B'])
print (df)
   A  B    C
0  a  z  0.2
1  a  y  0.3
2  a  x  0.5
3  b  z  0.1
4  b  y  0.9
5  b  x  0.3
6  c  z  0.7
7  c  y  0.8
8  c  x  0.6
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you very much! Can't give you +1 because I don't have the reputation for it, but I would give you +10 :D – Cookye Oct 21 '21 at 09:17