<p>IIUC,给定两个数据帧:</p>
<pre><code>dfa = pd.DataFrame({"col1": ["str{}".format(i) for i in range(10)],
"col2": ["str{}".format(chr(i)) for i in range(97,107)]})
col1 col2
0 str0 stra
1 str1 strb
2 str2 strc
3 str3 strd
4 str4 stre
5 str5 strf
6 str6 strg
7 str7 strh
8 str8 stri
9 str9 strj
dfb = pd.DataFrame({"col1": ["str0", "str1", "str1000"],
"col2": ["stra", "strb", "strc"]})
col1 col2
0 str0 stra
1 str1 strb
2 str1000 strc
</code></pre>
<p>您可以首先<code>transform</code>将前两列变成<code>tuples</code>(从而成为可散列对象),然后使用<code>isin</code>检查<code>df</code>a中是否存在这样的对,即</p>
<pre><code>>>> dfa.transform(tuple, 1)
0 (str0, stra)
1 (str1, strb)
2 (str2, strc)
3 (str3, strd)
4 (str4, stre)
5 (str5, strf)
6 (str6, strg)
7 (str7, strh)
8 (str8, stri)
9 (str9, strj)
</code></pre>
<p>所以呢</p>
<pre><code>df_final = dfb.loc[dfb.transform(tuple, 1).isin(dfa.transform(tuple, 1))]
</code></pre>
<p>这给了</p>
<pre><code> col1 col2
0 str0 stra
1 str1 strb
</code></pre>
<p>那就保存它<code>to_csv</code></p>
<pre><code>df_final.to_csv("result.csv")
</code></pre>
<hr/>
<p><em>(旁注:只要可能,尽量不要在数据帧中使用迭代。性能将很差,您将失去此库的真正功能)</em></p>