Skip to content
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 isNaN, 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 calledemergency_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, andrelationship. - 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
import json
import numpy as np
# Cargar las direcciones de las oficinas desde "office_addresses.csv"
office_addresses = pd.read_csv('datasets/office_addresses.csv')
# Cargar las direcciones de los empleados desde "employee_information.xlsx"
employee_addresses = pd.read_excel('datasets/employee_information.xlsx')
# Cargar los contactos de emergencia desde "employee_information.xlsx"
emergency_contacts = 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'])
# Cargar los roles, equipos y salarios de los empleados desde "employee_roles.json"
with open('datasets/employee_roles.json', 'r') as json_file:
employee_roles = pd.DataFrame.from_dict(json.load(json_file), orient='index')
# Comienza a trabajar con los datos estructurados...
df = employee_addresses.merge(emergency_contacts, on='employee_id')
df = df.merge(employee_roles, left_on='employee_id', right_index=True)
df = df.merge(office_addresses, left_on='employee_country', right_on='office_country', how = 'left')
for col in df.columns:
if col.startswith('office') and df[col].isnull().any():
df[col].fillna('Remote', inplace=True)
# Set index to employee_id
df = df.set_index('employee_id')
# df = df.rename(columns={'employee_last_name': 'last_name',
# 'employee_first_name': 'first_name'})
# Reorder columns
employees_final = df[['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# Cargar las direcciones de las oficinas desde "office_addresses.csv"
office_addresses = pd.read_csv('datasets/office_addresses.csv')
# Cargar las direcciones de los empleados desde "employee_information.xlsx"
employee_addresses = pd.read_excel('datasets/employee_information.xlsx')
# Cargar los contactos de emergencia desde "employee_information.xlsx"
emergency_contacts = 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'])office_addresses.head()employee_addresses.head()df = employee_addresses.merge(emergency_contacts, on='employee_id')
df = df.merge(employee_roles, left_on='employee_id', right_index=True)
df.head()
office_addresses.head()
df = df.merge(office_addresses, left_on='employee_country', right_on='office_country',
how = 'left')