python - 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.
解决方案
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]])
推荐阅读
- java - 接受来自服务器的java中的xml消息
- java - 具有基本身份验证 java 的 Http 请求
- python-2.7 - Python 从模块扩展类
- javascript - 从JS前端的图像缓冲区生成缩略图
- java - Android:应用程序图标从存根应用程序更新到最新版本后消失?
- ansible - Ansible 处理程序 - 独占重启或重新加载
- vb.net - 从代码隐藏有条件地隐藏/显示中继器内的按钮
- javascript - 按键后重置 lodash 去抖动
- html - href 被 ng-click 覆盖 - 移动
- css - full calendar_scheduler - 如何在垂直议程日设置最小宽度资源列并进行水平滚动