title: passing a variable into apply() in pandas
body: <p>I am having trouble getting the syntax right for applying a function to a dataframe. I am trying to create a new column in a dataframe by joining the strings in two other columns, passing in a separator. I get the error</p>
  <pre><code>TypeError: (&quot;apply_join() missing 1 required positional argument: 'sep'&quot;, 'occurred at index cases')
  </code></pre>
  <p>If I add sep to the apply_join() function call, that also fails:</p>
  <pre><code>  File &quot;unite.py&quot;, line 37, in unite
      tibble_extra = df[cols].apply(apply_join, sep)
  NameError: name 'sep' is not defined
  </code></pre>
  <pre class="lang-py prettyprint-override"><code>import pandas as pd
  from io import StringIO
  
  tibble3_csv = &quot;&quot;&quot;country,year,cases,population
  Afghanistan,1999,745,19987071
  Afghanistan,2000,2666,20595360
  Brazil,1999,37737,172006362
  Brazil,2000,80488,174504898
  China,1999,212258,1272915272
  China,2000,213766,1280428583&quot;&quot;&quot;
  with StringIO(tibble3_csv) as fp:
      tibble3 = pd.read_csv(fp)
  print(tibble3)
  
  def str_join_elements(x, sep=&quot;&quot;):
      assert type(sep) is str
      return sep.join((str(xi) for xi in x))
  
  def unite(df, cols, new_var, combine=str_join_elements):
      def apply_join(x, sep):
          joinstr = str_join(x, sep)
          return pd.Series({new_var[i]:s for i, s in enumerate(joinstr)})
    
      fixed_vars = df.columns.difference(cols)
      tibble = df[fixed_vars].copy()
      tibble_extra = df[cols].apply(apply_join)
    
      return pd.concat([tibble, tibble_extra], axis=1) 
  table3_again = unite(tibble3, ['cases', 'population'], 'rate', combine=lambda x: str_join_elements(x, &quot;/&quot;))
  print(table3_again)
  </code></pre>