First, you can list all files that starts with REC_
(If some of them are not .csv
then you need to check the extension as well). Then you can make a list of dataframes, each containing one REC_
file. Finally, pd.concat()
can be used to concatenate the dataframes. Here axis=0
means we add them over the rows (stacking them on top of each other vertically).
REC_file_1.csv
val_1, val_2
1, 2
2, 4
REC_file_2.csv
val_1, val_2
3, 6
4, 8
import os
import pandas as pd
# All files in directory
print(os.listdir())
# ['other_file_1.csv', 'REC_file_1.csv', 'REC_file_2.csv', 'script.py']
rec_file_names = [file for file in os.listdir() if file.startswith('REC_')]
print(rec_file_names) # ['REC_file_1.csv', 'REC_file_2.csv']
dataframes = []
for filename in rec_file_names:
dataframes.append(pd.read_csv(filename))
data_concated = pd.concat(dataframes, axis=0)
print(data_concated)
val_1 val_2
0 1 2
1 2 4
0 3 6
1 4 8