I found a solution without queue and threads. here is how i did it.
I installed the libraries
import pandas as pd
import numpy as np
import os
I afterwards added my pandas code where i treated the dataframe
def get_dataframe(file_name, sheet_name="CMO & KPI"):
df = pd.read_excel(file_name, sheet_name=sheet_name)
entity = df[df["Unnamed: 6"]=="Entity:"]["Unnamed: 9"].values[0]
df["Unnamed: 32"] = entity
data_entity = entity.split("-")
first_data = second_data = ""
if len(data_entity) > 1:
first_data = data_entity[0].strip()
second_data = data_entity[1].strip()
data = second_data.split(" ")
data1 = second_data
data2 = ""
if len(data) > 1:
data1 = data[0].strip()
data2 = data[1].strip()
df["Activity"] = first_data
df["Division"] = data1
df["Site"] = data2
date = df[df["Unnamed: 6"]=="Month:"]["Unnamed: 8"].values[0]
df["Unnamed: 36"] = date
df = df.drop([0, 1, 2, 3, 4, 5], axis=0)
df = df.drop(df.columns[[0, 1, 2, 3, 4, 5]], axis=1)
df.columns = df.iloc[0]
df = df.drop([6, 7, 8, 9, 10, 11, 12], axis=0)
df.rename(columns={list(df)[26]:'Entity'}, inplace=True)
df.rename(columns={list(df)[27]:'Activity'}, inplace=True)
df.rename(columns={list(df)[28]:'Division'}, inplace=True)
df.rename(columns={list(df)[29]:'Site'}, inplace=True)
df.rename(columns={list(df)[30]:'Date'}, inplace=True)
df.columns.values[0] = 'Index'
df = df.drop(labels=["colonne cachée","July","August"],axis=1)
df = pd.melt(df, id_vars=df.columns[[0, 23, 24, 25, 26, 27]], value_vars=df.columns[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]])
return df
I adressed python where to get the dataframe
and in order to concatenate, and use my function code to treat the dataframes I created a list of the files
files = []
dir_path = "./data"
for path in os.listdir(dir_path):
data_path = path.split(".")
if len(data_path) > 0 and data_path[-1].lower() == "xlsm":
files.append(path)
print(path)
than, i forced pandas code to treat the dataframes at the same
df_files = []
for name_file in files:
path_file = os.path.join(dir_path, name_file)
df = get_dataframe(path_file)
df_files.append(df)
print(name_file, list(df.columns), df.shape)
and finally i concatenated all the files to one file and export it as an excel file.
I read 25 files for the first time run, and took approximately 24 seconds to finish. the excel files are xlsm format in which there are macros and micros enabled formatted with visual basic.
I think it is the same logic but different path. Thank you