I have file names as below in a folder C/Downloads -
Mango001-003.csv
Mango004-006.csv
Mango007-100.csv
Applefruit.csv
Banana001-003.csv
Banana004-006.csv
How to import the fruits files separately and then join same fruit files together into a single file?
What's expected is one output for Mango, one for Apple & one for Banana
import os
import re
data_files = os.listdir(r'C:\Downloads')
def load_files(filenames):
# Pre-compile regex for code readability
regex = re.compile(r'Mango.*?.csv')
# Map filenames to match objects, filter out not matching names
matches = [m for m in map(regex.match, filenames) if m is not None]
li = []
for match in matches:
df = pd.read_csv(match, index_col=None, header=0, dtype=object)
li.append(df)
#Concatenating the data
frame = pd.concat(li, axis=0, ignore_index=True)
return (frame)
df = load_files(data_files)
print(df.shape)
df.head(2)
I am getting errors. In addition, it cannot be so complex, I must be doing something wrong.