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])
# 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[["Worker Name", "Position", "Description_4"]], on=["Worker Name"], how="left") # 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]
Fill in the missing Position and Project values with the respective matching values for each Worker Name
#worker_name_mapping = dict(merged_df[["Worker Name", "Position"]].values) #merged_df["Position"].fillna(merged_df["Worker Name"].map(worker_name_mapping), inplace=True)
#project_mapping = dict(merged_df[["Worker Name", "Project"]].values) #merged_df["Project"].fillna(merged_df["Worker Name"].map(project_mapping), inplace=True)
- Assistant
This script prompts the user to select input files, a mapping file, and an employees details file. It then reads the mapping file and employees details file using pandas and creates an empty dataframe to store merged data.
The script reads and merges the input files by adding new columns, renaming existing columns, and merging with the mapping file and employees details file. Unwanted columns are dropped, and the columns are reordered.
The merged dataframe is then written to an output Excel file. The file name is "merged_data.xlsx".
- User
How ever issue i am facing in code: merged_df = pd.merge(merged_df, employees_df[[“Worker Name”, “Position”, “Description_4”]], on=[“Worker Name”], how=“left”), is giving me more records 60K as output vs actual 20K needed in output. Please note there is duplicate in merged_df for Worker name and this is fine and i need the same as is with related values populated from employees_df
- Assistant
I see. To keep the duplicate records in
merged_dfwhile merging with theemployees_df, you can use themerge()function with the "left" join type, and then use thedrop_duplicates()function on the "Worker Name" column to remove any remaining duplicates. Here's the updated code:merged_df = pd.merge(merged_df, employees_df[["Worker Name", "Position", "Description_4"]], on=["Worker Name"], how="left") merged_df = merged_df.drop_duplicates(subset=["Worker Name"])This will ensure that only unique records for each "Worker Name" are kept in the
merged_df.