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 import numpy as np
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])
Convert the “Worker” and “Personal Number” columns to numeric data type
#employees_df["Personal Number"] = pd.to_numeric(employees_df["Personal Number"], errors="coerce") #for col in ["Worker"]: #for file in input_files: #df = pd.read_excel(file) #df[col] = pd.to_numeric(df[col], errors="coerce")
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]) # Convert the “Personal Number” column in the employees dataframe to string type #employees_df["Personal Number"] = employees_df["Personal Number"].astype(str) # Merge with the employees details file based on “Worker Name” and “Personal Number” columns df = pd.merge(df, employees_df[["English Name", "Position"]], left_on=["Worker Name"], right_on=["English Name"], how="left") merged_df = pd.concat([merged_df, df]) # Fill in the missing Position values in merged_df with the values from dfFill in the missing Position values in merged_df with the values from df
Write the merged dataframe to an output Excel file
output_file = "merged_data.xlsx" merged_df.to_excel(output_file, index=False)
print("Merged dataframe has been written to", output_file)
- Assistant
This code is a script that takes input files, a mapping file, and an employees file, and merges them into a single dataframe. The merged dataframe is then written to an output Excel file.
The script uses the pandas library to read and manipulate Excel files, and the tkinter library to create a file selection window for the user to select the input files, mapping file, and employees file.
- User
code taking lot of time to run, please fine tune the code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.