Almost all of the answers here are either unnecessarily complex (glob pattern matching) or rely on additional 3rd partythird-party libraries. You can do this in 2two lines using everything Pandas and pythonPython (all versions) already have built in.
For a few files - 1 linerone-liner
df = pd.concat(map(pd.read_csv, ['d1.csv', 'd2.csv','d3.csv']))
For many files
import os
filepaths = [f for f in os.listdir(".") if f.endswith('.csv')]
df = pd.concat(map(pd.read_csv, filepaths))
For No Headers
If you have specific things you want to change with pd.read_csv (i.e., no headers) you can make a separate function and call that with your map:
def f(i):
return pd.read_csv(i, header=None)
df = pd.concat(map(f, filepaths))
This pandas line, which sets the df, utilizes 3three things:
- Python's map (function, iterable) sends to the function (the
pd.read_csv()) the iterable (our list) which is every csvCSV element in filepaths). - Panda's read_csv() function reads in each CSV file as normal.
- Panda's concat() brings all these under one df variable.