title: How can I add a new column based on two dataframes and conditions for big data?
body: <p>How can I add a new column based on two dataframes and conditions?</p>
  <p>For example, if df2['x'] is between df1['x']±10 and df2['y'] is between df1['y']±10 and for same year, give 1 otherwise 0.</p>
  <p>The codes below worked with relatively small data. But when I  used it for big data (df1=210.548 rows, df2= 301,661 rows), I got error message: &quot;The kernel appears to have died. It will restart automatically&quot;.</p>
  <p>Are there any other ways to solve it? You can find sample dataframes and expected output in this question: <a href="https://stackoverflow.com/questions/71328410/how-can-i-add-a-new-column-based-on-two-dataframes-and-conditions/71329046#71329046">How can I add a new column based on two dataframes and conditions</a></p>
  <pre class="lang-py prettyprint-override"><code>merged = df1.merge(df2, on=&quot;year&quot;, how=&quot;left&quot;, suffixes=(&quot;1&quot;, &quot;2&quot;))
  merged[&quot;f&quot;] = (
      (merged.x2 &gt;= merged.x1 - 10)
      &amp; (merged.x2 &lt; merged.x1 + 10)
      &amp; (merged.y2 &gt;= merged.y1 - 10)
      &amp; (merged.y2 &lt; merged.y1 + 10)
  )
  dataset = (
      merged.groupby([&quot;x1&quot;, &quot;y1&quot;, &quot;year1&quot;, &quot;a&quot;, &quot;b&quot;, &quot;f&quot;])
      .f.any()
      .astype(int)
      .reset_index()
  )
  </code></pre>