擅长:python、mysql、java
<p><strong>编辑</strong>:</p>
<p>如果所有右数据帧中的url都是唯一的,那么可以将右数据帧作为由<code>url</code>索引的一系列<code>class</code>,然后通过索引得到左边每个url的类。在</p>
<pre><code>from pandas import *
left = DataFrame({'url': ['foo.com', 'bar.com', 'foo.com', 'tmp', 'foo.com'], 'action': [0, 1, 0, 2, 4]})
left["klass"] = NaN
right1 = DataFrame({'url': ['foo.com', 'tmp'], 'klass': [10, 20]})
right2 = DataFrame({'url': ['bar.com'], 'klass': [30]})
left["klass"] = left.klass.combine_first(right1.set_index('url').klass[left.url].reset_index(drop=True))
left["klass"] = left.klass.combine_first(right2.set_index('url').klass[left.url].reset_index(drop=True))
print left
</code></pre>
<p>这是你想要的吗?在</p>
^{2}$
<p>输出:</p>
<pre><code> action url class
0 0 foo.com 0
1 1 foo.com 0
2 0 bar.com 1
</code></pre>
<p>如果左边的class列不是全部为NaN,则可以将其与结果合并。在</p>