Project: Consolidating Employee Data
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    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" }, ... }

    1. Reading in the datasets.

    • Read in four files: data/office_addresses.csv, two sheets from data/employee_information.xlsx, and data/employee_roles.json.
    import pandas as pd
    
    df1 = pd.read_csv('data/office_addresses.csv')
    df2 = pd.read_excel('data/employee_information.xlsx' )
    df3 = pd.read_excel('data/employee_information.xlsx',sheet_name="emergency_contacts",names= ['employee_id', 'last_name', 'first_name', 'emergency_contact', 'emergency_contact_number','relationship'] )
    df4 = pd.read_json('data/employee_roles.json' , orient = 'index')
    

    2. Merging the DataFrames

    Merge all four datasets ready for cleaning.

    employees = df1.merge(df2, how='left', right_on='employee_country' , left_on='office_country')
    employees = employees.merge(df4 , left_on='employee_id' , right_on = df4.index )
    employees = employees.merge(df3 ,  on ='employee_id' )
                                
    

    3. Cleaning and formatting the data.

    • Fill null values with "Remote", rename columns, subset the DataFrame, and set the index.
    for index , row in employees.iterrows(): 
        if 'Office%' in row:
            employees[row].fillna('Remote' , inplace = True)
            
    
    • Drop the columns called "last_name" and "first_name".
    employees = employees.drop([ "last_name" ,"first_name"] , axis = 1)
    • Rename the remaining name columns, removing "employee_" so they are called "last_name" and "first_name".
    employees.rename(columns = { 'employee_last_name':'last_name', 'employee_first_name':'first_name' }, inplace=True)
    
    employees.columns
    • Subsetting and reindexing the DataFrame