import pandas as pd from tkinter import Tk from tkinter.filedialog import…
- User
import pandas as pd from tkinter import Tk from tkinter.filedialog import askopenfilenames import os
Open a Tk window
Tk().withdraw()
Prompt the user to select input files
input_files = askopenfilenames(title="Select input files", filetypes=[("Excel Files", "*.xlsx")])
Prompt the user to select the “Staff Gl Mapping.xlsx” file
mapping_file = askopenfilenames(title="Select Staff Gl Mapping file", filetypes=[("Excel Files", ".xlsx")])
Prompt the user to select the “Employees Details Report old.xlsx” file
employees_file = askopenfilenames(title="Select Employees Details Report file", filetypes=[("Excel Files", ".xlsx")])
Read the mapping file
mapping_df = pd.read_excel(mapping_file[0])
Read the employees details file
employees_df = pd.read_excel(employees_file[0])
Create an empty dataframe to store merged data
merged_df = pd.DataFrame()
Read and merge the input files
for file in input_files: df = pd.read_excel(file) df["Project Description"] = df["Name"].str.split("|").str[2] df["Worker Name"] = df["Name"].str.split("|").str[4] df["Current"] = df["Debit"] - df["Credit"] df = df.rename(columns={"Closing balance": "Cumulative"}) file_name, _ = os.path.splitext(os.path.basename(file)) df["Date"] = file_name #merged_df = pd.concat([merged_df, df])
code# Merge with the mapping file based on “Main account” column df = pd.merge(df, mapping_df[["Main account", "Name y"]], left_on="MainAccount", right_on="Main account", how="left") merged_df = pd.concat([merged_df, df]) # Merge with the employees details file based on “Worker Name” and “Personal Number” columns merged_df = pd.merge(merged_df, employees_df[["English Name", "Position", "Description_4"]], left_on=["Worker Name"], right_on=["English Name"], how="inner") # Remove the unwanted columnsunwanted_cols = ["MainAccount", "Division", "Site", "Name", "Opening balance", "Main account"] merged_df = merged_df.drop(columns=unwanted_cols)
Rename the columns
merged_df = merged_df.rename(columns={ "Projects": "Project ID", "Worker": "Worker ID", "Name y": "Name", "Description_4": "Project" })
Reorder the columns
new_order = ["Project ID", "Project Description", "Date", "Worker ID", "Worker Name", "Name", "Current", "Cumulative"] #, "Position", "Project"] merged_df = merged_df[new_order]
- Assistant
The given code imports the required libraries and prompts the user to select input files, "Staff Gl Mapping.xlsx" file, and "Employees Details Report old.xlsx" file using tkinter file dialog. The code then reads the mapping file and employees details file into pandas dataframes.
An empty dataframe called "merged_df" is created to store the merged data. The code then loops through the input files and reads each file into a dataframe. It extracts the project description, worker name, and calculates the "Current" column as the difference between debit and credit. It also renames the "Closing balance" column to "Cumulative" and adds a "Date" column extracted from the filename.
The code then merges the dataframe with the mapping file based on the "Main account" column, and with the employees details file based on "Worker Name" and "Personal Number" columns. It removes the unwanted columns and renames the remaining columns. The columns are reordered and the merged dataframe is saved to an output Excel file named "merged_data.xlsx".
Note: There are some commented lines of code related to filling in missing values for "Position" and "Project" columns. These lines can be uncommented and modified if needed.
- User
Traceback (most recent call last): File "c:\Users\Dell\Desktop\Desktop Files\ERP\Merge Files Task\mergefiles_interactive.py", line 43, in merged_df = pd.merge(merged_df, employees_df[["English Name", "Position", "Description_4"]], left_on=["Worker Name"], right_on=["English Name"], how="left") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Dell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pandas\core\reshape\merge.py", line 148, in merge op = _MergeOperation( ^^^^^^^^^^^^^^^^ File "C:\Users\Dell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pandas\core\reshape\merge.py", line 737, in init ) = self._get_merge_keys() ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Dell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pandas\core\reshape\merge.py", line 1221, in _get_merge_keys left_keys.append(left._get_label_or_level_values(lk)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Dell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pandas\core\generic.py", line 1778, in _get_label_or_level_values raise KeyError(key) KeyError: 'Worker Name'
- Assistant
It seems that there is an error in the code at the line:
pythonmerged_df = pd.merge(merged_df, employees_df[["English Name", "Position", "Description_4"]], left_on=["Worker Name"], right_on=["English Name"], how="left")The error message suggests that the "Worker Name" column is not present in the merged_df dataframe. A possible reason for this error could be that the previous line of code where the "Worker Name" column is added to the "df" dataframe is commented out:
python# df["Worker Name"] = df["Name"].str.split("|").str[4]To fix this issue, you should uncomment this line of code and rerun the program. It will add the "Worker Name" column to the "df" dataframe before merging it with the main dataframe.