title: How to append multiple CSV files and add an additional column indicating file name in Python?
body: <p>I have over 20 CSV files in a single folder.  All files have the same structure, they just represent different days. </p>
  
  <p>Example:</p>
  
  <p>Day01.csv</p>
  
  <p>Day02.csv</p>
  
  <p>Day03.csv</p>
  
  <p>Day04.csv (and so on...)</p>
  
  <p>The files contain just two numeric columns: x and y. I would like to append all of these csv files together into one large file and add a column for the file name (day). I have explored similar examples to generate the following code but this code adds each y to a separate column (Y1, Y2, Y3, Y4...and so on).  I would like to simply have this appended file as three columns: x, y, file name. How can I modify the code to do the proper append? </p>
  
  <p>I have tried the code from this example: <a href="https://stackoverflow.com/questions/42756696/read-multiple-csv-files-and-add-filename-as-new-column-in-pandas">Read multiple csv files and Add filename as new column in pandas</a></p>
  
  <pre><code>import pandas as pd
  import os
  os.chdir('C:....path to my folder')
  files = os.listdir()
  df = pd.concat([pd.read_csv(fp).assign(New=os.path.basename(fp)) for fp in files])
  </code></pre>
  
  <p>However, this code does not append all Y values under one column. (all other aspects seem to work, however). Can someone help with the code so that all Y values are under a single column?</p>