擅长:python、mysql、java
<p>我认为当您写回<code>df</code>时,会发生太多的查找。看起来每个<code>df.loc[row_index, ...]</code>需要检查的行数与URL的总数(大小为<code>df.url</code>)相同。这意味着,首先至少要查看所有行一次以找到唯一的url,然后对于每个url,您都要再次查找匹配的行,然后再对每次写入进行一次。因此,假设<code>unique</code>只进行一次完整扫描,则平均扫描<code>1+N+(5N/2)</code>次。你真的只需要一次。在</p>
<p>除非有大量的重复,否则可以忽略重复项,逐行遍历<code>df</code>,并确保每次迭代都使用整数索引。(<code>.iloc</code>)如果不在行中存储其他数据,也可以一次分配所有字段:</p>
<pre><code>df.iloc[idx] = {'protocol': ..., 'domain': ..., ...}
</code></pre>