title: Pandas merge in a function
body: <p>I am trying to use pandas merge in a function as shown below.</p>
  <p>Here is what I am trying to use :</p>
  <pre><code>test = inv_level(left, right, 'user_id', 'new_id', 'left') #left.merge(right, on='user_id', how='left')
  
  </code></pre>
  <p>where inv_level is a function as defined below</p>
  <pre><code>def inv_level ( child_inv, parent_inv, lefton, righton, how ):
      level_inv = pd.merge(parent_inv, child_inv, left_on=lefton, right_on=righton, how=how)
      return level_inv
  
  </code></pre>
  <p>When I use merge directly without a function it works fine. But, when I use merge in a function and call the function passing parameters, it throws an error. I am not sure what exactly is the issue. I appreciate your input/support.</p>
  <p>File &quot;C:\temp\env\3.8.6\lib\site-packages\pandas\core\reshape\merge.py&quot;, line 1005, in _get_merge_keys right_keys.append(right._get_label_or_level_values(rk)) File &quot;C:\temp\env\3.8.6\lib\site-packages\pandas\core\generic.py&quot;, line 1563, in _get_label_or_level_values raise KeyError(key) KeyError: 'new_id'</p>
  <p>Thanks,</p>
  <p>Following is the complete program.</p>
  <pre><code>import numpy as np
  import pandas as pd
  
  
  def inv_level ( child_inv, parent_inv, lefton, righton, how ):
      level_inv = pd.merge(parent_inv, child_inv, left_on=lefton, right_on=righton, how=how)
      return level_inv
  
  
  def main(event, context):
      np.random.seed(0)
      # transactions
      left = pd.DataFrame({'transaction_id': ['A', 'B', 'C', 'D'], 
                          'user_id': ['Peter', 'John', 'John', 'Anna'],
                          'value': np.random.randn(4),
                      })
      # users
      right = pd.DataFrame({'new_id': ['Paul', 'Mary', 'John', 'Anna'],
                          'favorite_color': ['blue', 'blue', 'red', 
                                              np.NaN],
                          })
  
  '''
      test = inv_level(left, right, 'user_id', 'new_id', 'left') #left.merge(right, on='user_id', how='left')
       The above throws an error
  '''
      test = pd.merge(left, right, left_on='user_id', right_on='new_id', how='left') 
  
      print(test)
  
  if __name__ == &quot;__main__&quot;:
      main(&quot;&quot;, &quot;&quot;)
  </code></pre>