I have multiple csv files, and I'm trying to concatenate the desired columns for all csv files in the folder.
Here's my code:
import pandas as pd
import numpy as np
import os
path_dataset = r"C:\Users\KL"
def get_file(path_dataset):
files = os.listdir(path_dataset)
files.sort()
file_list = []
for file in files:
path = path_dataset + "\\" + file
if (file.startswith("OS")) and (file.endswith(".csv")):
file_list.append(path)
return file_list
read_columns = ["LX", "LY", "LZ", "LA"]
read_files = get_file(path_dataset)
for file in read_files:
df = pd.read_csv(file, usecols=read_columns)
all_df = [df]
Concat_table = pd.concat(all_df, axis=0)
Concat_table = Concat_table.sort_values(["LX", "LY", "LZ", "LA"])
Concat_table.to_csv(os.path.join(path_dataset, "Concate_all.csv"), index=False)
I was only able to read one file but not for all csv files. How can I solve this? Thank you.