title: NameError: ("free variable 'type' referenced before assignment in enclosing scope"
body: <p>I am using this code </p>
  
  <pre><code>dof = {i: dframe.apply(lambda x: x.str[i - 1] if type(x.iat[0]) == list else x) for i in range(1, 7)}
  </code></pre>
  
  <p>to convert a dataframe of lists to a dict of dataframes, as given in this <a href="https://stackoverflow.com/questions/43776297/convert-pandas-dataframe-of-lists-to-dict-of-dataframes/43776567?noredirect=1#comment74597447_43776567">thread</a>. The line works when I use it in a console, but when I include it in my script I get this message:</p>
  
  <p><code>NameError: ("free variable 'type' referenced before assignment in enclosing scope", 'occurred at index duration')</code></p>
  
  <p>Without posting my whole script, can you suggest what I should be looking for that has generated this error? There is no use of <code>type</code> as a variable in the script, nor is <code>x</code> or <code>i</code> used elsewhere as a variable.</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/PYTHONprojects/DataDump/log_latest_from_api.py", line 138, in main
      dof = {i: dframe.apply(lambda x: x.str[i - 1] if type(x.iat[0]) is list else x) for i in range(1, 7)}
    File "C:/PYTHONprojects/DataDump/log_latest_from_api.py", line 138, in &lt;dictcomp&gt;
      dof = {i: dframe.apply(lambda x: x.str[i - 1] if type(x.iat[0]) is list else x) for i in range(1, 7)}
    File "C:\Users\...\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4061, in apply
      return self._apply_standard(f, axis, reduce=reduce)
    File "C:\Users\...\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4157, in _apply_standard
      results[i] = func(v)
    File "C:/PYTHONprojects/DataDump/log_latest_from_api.py", line 138, in &lt;lambda&gt;
      dof = {i: dframe.apply(lambda x: x.str[i - 1] if type(x.iat[0]) is list else x) for i in range(1, 7)}
  NameError: ("free variable 'type' referenced before assignment in enclosing scope", 'occurred at index duration'
  </code></pre>