title: Python dataframe check if a value in a column dataframe is within a range of values reported in another dataframe
body: <p>Apology if the problemis trivial but as a python newby I wasn't able to find the right solution.</p>
  
  <p>I have two dataframes and I need to add a column to the first dataframe that is true if a certain value of the first dataframe is between two values of the second dataframe otherwise false.</p>
  
  <p>for example:</p>
  
  <pre><code>first_df = pd.DataFrame({'code1':[1,1,2,2,3,1,1],'code2':[10,22,15,15,7,130,2]})
  second_df = pd.DataFrame({'code1':[1,1,2,2,3,1,1],'code2_start':[5,20,11,11,5,110,220],'code2_end':[15,25,20,20,10,120,230]})
  
  first_df
  
    code1 code2
  0   1   10
  1   1   22
  2   2   15
  3   2   15
  4   3   7
  5   1   130
  6   1   2
  
  second_df
  
    code1 code2_end   code2_start
  0   1   15          5
  1   1   25          20
  2   2   20          11
  3   2   20          11
  4   3   10          5
  5   1   120         110
  6   1   230         220
  </code></pre>
  
  <p>For each row in the first dataframe I should check if the value reported in the code2 columne is between one of the possible range identified by the row of the second dataframe second_df for example:</p>
  
  <p>in row 1 of <code>first_df</code> <code>code1=1</code> and <code>code2=22</code></p>
  
  <p>checking <code>second_df</code> I have 4 rows with <code>code1=1</code>, <code>rows 0,1,5</code> and <code>6</code>, the value <code>code2=22</code> is in the interval identified by <code>code2_start=20</code> and <code>code2_end=25</code> so the function should return <code>True</code>.</p>
  
  <p>Considering an example where the function should return False,</p>
  
  <p>in row 5 of <code>first_df</code> <code>code1=1</code> and <code>code2=130</code></p>
  
  <p>but there is no interval containing 130 where <code>code1=1</code></p>
  
  <p>I have tried to use this function</p>
  
  <pre><code>def check(first_df,second_df):
      for i in range(len(first_df):
          return ((second_df.code2_start &lt;= first_df.code2[i]) &amp; (second_df.code2_end &lt;= first_df.code2[i]) &amp; (second_df.code1 == first_df.code1[i])).any()
  </code></pre>
  
  <p>and to vectorize it</p>
  
  <pre><code>first_df['output'] = np.vectorize(check)(first_df, second_df)
  </code></pre>
  
  <p>but obviously with no success.</p>
  
  <p>I would be happy for any input you could provide.</p>
  
  <p>thx.</p>
  
  <p>A.</p>
  
  <p>As a practical example:</p>
  
  <pre><code>first_df.code1[0] = 1
  </code></pre>
  
  <p>therefore I need to search on second_df all the istances where</p>
  
  <pre><code>second_df.code1 == first_df.code1[0]
  0     True
  1     True
  2    False
  3    False
  4    False
  5     True
  6     True
  </code></pre>
  
  <p>for the instances 0,1,5,6 where the status is True I need to check if the value</p>
  
  <pre><code>first_df.code2[0]
  10
  </code></pre>
  
  <p>is between one of the range identified by</p>
  
  <pre><code>second_df[second_df.code1 == first_df.code1[0]][['code2_start','code2_end']]
      code2_start code2_end
  0   5           15
  1   20          25
  5   110         120
  6   220         230
  </code></pre>
  
  <p>since the value of first_df.code2[0] is 10 it is between 5 and 15 so the range identified by row 0 therefore my function should return True. In case of first_df.code1[6] the value vould still be 1 therefore the range table would be still the same above but first_df.code2[6] is 2 in this case and there is no interval containing 2 therefore the resut should be False.</p>