title: Read multiple *.txt files into Pandas Dataframe with filename as column header
body: <p>I am trying to import a set of *.txt files. I need to import the files into successive columns of a Pandas DataFrame in Python.</p>
  
  <p>Requirements and Background information:</p>
  
  <ol>
  <li>Each file has one column of numbers</li>
  <li>No headers are present in the files</li>
  <li>Positive and negative integers are possible</li>
  <li>The size of all the *.txt files is the same</li>
  <li>The columns of the DataFrame must have the name of file (without extension) as the header</li>
  <li>The number of files is not known ahead of time</li>
  </ol>
  
  <p>Here is one sample *.txt file. All the others have the same format.</p>
  
  <pre><code>16
  54
  -314
  1
  15
  4
  153
  86
  4
  64
  373
  3
  434
  31
  93
  53
  873
  43
  11
  533
  46
  </code></pre>
  
  <p>Here is my attempt:</p>
  
  <pre><code>import pandas as pd
  import os
  import glob
  
  # Step 1: get a list of all csv files in target directory
  my_dir = "C:\\Python27\Files\\"
  filelist = []
  filesList = []
  os.chdir( my_dir )
  
  # Step 2: Build up list of files:
  for files in glob.glob("*.txt"):
      fileName, fileExtension = os.path.splitext(files)
      filelist.append(fileName) #filename without extension
      filesList.append(files) #filename with extension
  
  # Step 3: Build up DataFrame:
  df = pd.DataFrame()
  for ijk in filelist:
      frame = pd.read_csv(filesList[ijk])
      df = df.append(frame)
  print df
  </code></pre>
  
  <p>Steps 1 and 2 work. I am having problems with step 3. I get the following error message:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Python27\TextFile.py", line 26, in &lt;module&gt;
      frame = pd.read_csv(filesList[ijk])
  TypeError: list indices must be integers, not str
  </code></pre>
  
  <p>Question:
  Is there a better way to load these *.txt files into a Pandas dataframe? Why does read_csv not accept strings for file names?</p>