I want to find the values of col1
and col2
where the col1
and col2
of the first dataframe are both in the second dataframe.
这些行应该在结果数据框中:
披萨,男孩
披萨,女孩
冰淇淋,男孩
因为所有三行都在第一个和第二个数据框中。
我怎么可能做到这一点?我正在考虑使用 isin
,但是当我必须考虑多个列时,我不确定如何使用它。
原文由 Jun Jang 发布,翻译遵循 CC BY-SA 4.0 许可协议
对
col1
和col2
执行 内部合并:产量
reset_index
和set_index
调用的目的是保留df2
的索引,如您发布的所需结果。如果索引不重要,那么就足够了。
Alternatively, you could construct
MultiIndex
s out of thecol1
andcol2
columns, and then call theMultiIndex.isin
method :产量