title: pd.Series - "Filename" KeyError
body: <p>I am having trouble running a script for getting counts of predictions from csv files at a given directory. The format of the csv looks like this:</p>
  <p><a href="https://i.stack.imgur.com/wSATc.png" rel="nofollow noreferrer">Sample data</a></p>
  <p>and the code is the following:</p>
  <pre><code>    import os
      from glob import glob
      import pandas as pd
  
      def get_count(distribution, keyname):
      try:
          count = distribution[keyname]
      except KeyError:
          count = 0
      return count
  
      main_path = &quot;K:\\...\\folder_name&quot;
  
      folder_paths = glob(&quot;%s\\*&quot; % main_path)
      data = []
  
      for path in folder_paths:
      file_name = os.path.splitext(os.path.basename(path))[0]
      results = pd.read_csv(path, error_bad_lines=False)results['Label'] = pd.Series(results['Filename'].str.split(&quot;\\&quot;).str[0])
  
      distribution = results.Predictions.value_counts()
      print(distribution)
  
      num_of_x = get_count(distribution, &quot;x&quot;)
      num_of_y = get_count(distribution,&quot;y&quot;)
      num_of_z = get_count(distribution,&quot;z&quot;) 
  
      d = {&quot;filename&quot;: file_name, &quot;x&quot;: num_of_x, &quot;y&quot;: num_of_y, &quot;z&quot;: num_of_z}
      data.append(d)
      df = pd.DataFrame(data=data)
      df.to_csv(os.path.join(main_path,&quot;summary_counts.csv&quot;), index=False)
  </code></pre>
  <p>the output error is Keyerror: &quot;Filename&quot; reffering to the pd.Series function, anyone would know how to solve this?</p>
  <p>I am using Python 3.7.3 and pandas 1.0.5 and I am a beginner in programming...</p>
  <p>Many thanks in advance</p>
  <pre><code>During handling of the above exception, another exception occurred:
  Traceback (most recent call last):
  
  File &quot;.\save_counts.py&quot;, line 24, in &lt;module&gt;
  results['Label'] = pd.Series(results['Filename'].str.split(&quot;\\&quot;).str[0])
  File &quot;K:\...\lib\site-packages\pandas\core\frame.py
  &quot;, line 2800, in __getitem__
  indexer = self.columns.get_loc(key)
  File &quot;K:\...\site-packages\pandas\core\indexes\
  base.py&quot;, line 2648, in get_loc
  return self._engine.get_loc(self._maybe_cast_indexer(key))
  File &quot;pandas\_libs\index.pyx&quot;, line 111, in           pandas._libs.index.IndexEngine.get
  _loc
   File &quot;pandas\_libs\index.pyx&quot;, line 138, in    pandas._libs.index.IndexEngine.get
   _loc
  File &quot;pandas\_libs\hashtable_class_helper.pxi&quot;, line 1619, in pandas._libs.has
  htable.PyObjectHashTable.get_item
  File &quot;pandas\_libs\hashtable_class_helper.pxi&quot;, line 1627, in pandas._libs.has
  htable.PyObjectHashTable.get_item
  KeyError: 'Filename'
  </code></pre>