Skip to content
Advertisement

Two DataFrames, find index of second one where values of two columns match up from first

I have two pandas DataFrames as pictured.

DF1:

              0     1     2     3    4
0    2021-02-24  19.0  35.0  34.5  0.0
1    2021-02-24  20.0  33.0  34.5  0.0
2    2021-02-24  21.0  30.0  34.5  0.0
3    2021-02-24  22.0  28.0  34.5  0.0
4    2021-02-24  23.0  27.0  34.5  0.0
..          ...   ...   ...   ...  ...
163  2021-03-03  14.0  43.0  25.0  0.0
164  2021-03-03  15.0  47.0  25.0  0.0
165  2021-03-03  16.0  50.0  25.0  0.0
166  2021-03-03  17.0  51.0  25.0  0.0
167  2021-03-03  18.0  51.0  25.0  0.0

[168 rows x 5 columns]

DF2 (192 x 7):

              0    1     2     3     4     5    6
3171 2021-02-24  3.0   1.0   1.0  29.0  37.0  0.0
3172 2021-02-24  3.0   2.0   2.0  28.0  37.0  0.0
3173 2021-02-24  3.0   3.0   3.0  26.0  37.0  0.0
3174 2021-02-24  3.0   4.0   4.0  25.0  37.0  0.0
3175 2021-02-24  3.0   5.0   5.0  25.0  37.0  0.0
3176 2021-02-24  3.0   6.0   6.0  24.0  37.0  0.0
3177 2021-02-24  3.0   7.0   7.0  22.0  37.0  0.0
3178 2021-02-24  3.0   8.0   8.0  21.0  37.0  0.0
3179 2021-02-24  3.0   9.0   9.0  21.0  37.0  0.0
3180 2021-02-24  3.0  10.0  10.0  22.0  37.0  0.0
3181 2021-02-24  3.0  11.0  11.0  23.0  37.0  0.0
3182 2021-02-24  3.0  12.0  12.0  26.0  37.0  0.0
3183 2021-02-24  3.0  13.0  13.0  29.0  37.0  0.0
3184 2021-02-24  3.0  14.0  14.0  32.0  37.0  0.0
3185 2021-02-24  3.0  15.0  15.0  33.0  37.0  0.0
3186 2021-02-24  3.0  16.0  16.0  35.0  37.0  0.0
3187 2021-02-24  3.0  17.0  17.0  35.0  37.0  0.0
3188 2021-02-24  3.0  18.0  18.0  34.0  37.0  0.0
3189 2021-02-24  3.0  19.0  19.0  33.0  37.0  0.0
3190 2021-02-24  3.0  20.0  20.0  31.0  37.0  0.0

I want to find the index value of DF2 where df1[0] & df1[1] match df2[0] & df2[2]. For more detail, this would be represented above as starting at index 3188 of DF2. DF1 values will be dynamically changing as DF2 stays constant.

Edit: Just noticed that there was an error in my logic. I meant DF1[0] == DF2[0] and DF1[1]==DF2[2]. I have updated above accordingly.

Advertisement

Answer

You can find the first place where DF2 matches DF1 with offset = DF2.loc[ (DF2[0] == DF1.loc[0,0]) & (DF2[1] == DF1.loc[0,1])] ][0]. If you want to then test whether the rest of the columns match up, you’ll have to get them to have the same shape and the same index. In this case, the part of DF2 after the matching row is smaller than DF1 overall, so one way to do this is as follows (if DF1 were smaller, the process would be a bit different):

 DF2_tail = DF2.loc[offset:].copy()
 num_rows = DF2_tail.shape[0]
 DF2_tail.index = range(num_rows)
 DF1.loc[:num_rows,[0,1]].eq(DF2_tail[[0,1]])
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement