I have multiple csv files in a folder. The objective is to append the csv files into a single pd frame.
The question is how can we use pandas to concatenate all files in the folder, but at the same time associate specific keys with each of the pieces of the chopped up DataFrame using the keys argument: keys.
This means that we can now select out each chunk by key:
For example, Given two csv files in a folder, each of the csv have 3 column (A, B, C) and two rows.
CSV File: Book1
A0 B0 C0
A1 B1 C1
and
CSV File: Book2
A2 B2 C2
A3 B3 C3
The expected frames as shown in the figure.
Notice the index Book1 and Book2, on the left column. This name comes from the said csv file.
So far, I have the following code
# match the pattern ‘csv’ in the folder
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
But where under the following line of code I need to change to achieve the said objective?
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
The reason why adding this keys is, to make easy access in the future. This usually can be achieve from
.loc['Book1']
