Monica Nava/

Project: Consolidating Employee Data


You just got hired as the first and only data practitioner at a small business experiencing exponential growth. The company needs more structured processes, guidelines, and standards. Your first mission is to structure the human resources data. The data is currently scattered across teams and files and comes in various formats: Excel files, CSVs, JSON files...

You'll work with the following data in the datasets folder:

  • Office addresses are currently saved in office_addresses.csv. If the value for office is NaN, then the employee is remote.
  • Employee addresses are saved on the first tab of employee_information.xlsx.
  • Employee emergency contacts are saved on the second tab of employee_information.xlsx; this tab is called emergency_contacts. However, this sheet was edited at some point, and the headers were removed! The HR manager let you know that they should be: employee_id, last_name, first_name, emergency_contact, emergency_contact_number, and relationship.
  • Employee roles, teams, and salaries have been exported from the company's human resources management system into a JSON file titled employee_roles.json. Here are the first few lines of that file:
{"A2R5H9": { "title": "CEO", "monthly_salary": "$4500", "team": "Leadership" }, ... }
import pandas as pd
#read office addresses
office_file = 'datasets/office_addresses.csv'
office_data = pd.read_csv(office_file)
#declare list of columns to keep addresses
#to reference the other dataset (employee information)
adresses_cols=['employee_id','employee_country','employee_city','employee_street', 'employee_street_number']

#read employee information regarding employee addresses
employeeinfo_file = 'datasets/employee_information.xlsx'
employeeaddresses = pd.read_excel(employeeinfo_file, usecols=adresses_cols)

#read employee information regarding emergency data
employee_emergency_data = pd.read_excel('datasets/employee_information.xlsx', sheet_name='emergency_contacts', header=None, names=['employee_id','last_name','first_name','emergency_contact','emergency_contact_number','relationship'])

#read employee roles
employeerole_file= 'datasets/employee_roles.json'
employeerole_data = pd.read_json(employeerole_file, orient="index")
#df_transpose.rename(columns = {'': 'employee_id'}, inplace = True) 

#merge addresses with offices
employees = employeeaddresses.merge(office_data, left_on='employee_country', right_on='office_country', how='left')

#merge employees with roles
employees= employees.merge(employeerole_data, left_on='employee_id', right_on=employeerole_data.index)

#merge employees with emergency contact3
employees = employees.merge(employee_emergency_data, on='employee_id')

#change nan to remote in any column starting with office
#list of office columns
for col in  ['office','office_country','office_city','office_street','office_street_number']:employees[col].fillna('Remote', inplace=True)

#columns on final dataframe
final_columns = ['employee_id','first_name','last_name','employee_country', 'employee_city', 'employee_street', 'employee_street_number', 'emergency_contact', 'emergency_contact_number', 'relationship', 'monthly_salary', 'team', 'title', 'office', 'office_country', 'office_city', 'office_street', 'office_street_number']
employees_final = employees[final_columns]

#set index
employees_final.set_index('employee_id', inplace=True)
  • AI Chat
  • Code