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" }, ... }
    import pandas as pd
    
    #Defining Files
    file1 = 'datasets/office_addresses.csv'
    file2 = 'datasets/employee_information.xlsx'
    file3 = 'datasets/employee_roles.json'
    #Reading and transforming office addresses csv
    df_office_addresses = pd.read_csv(file1)
    df_office_addresses.head()
    #Reading and Transforming spreadsheets
    df_employee_address = pd.read_excel(file2,sheet_name=0,index_col='employee_id')
    df_employee_address.head()
    df_emergency_contacts = pd.read_excel(file2,sheet_name='emergency_contacts',header=None, names=['employee_id', 'last_name', 'first_name', 'emergency_contact', 'emergency_contact_number', 'relationship'],index_col='employee_id')
    df_emergency_contacts.head()
    df_employee_roles_teams_salary = pd.read_json(file3,orient='index')
    df_employee_roles_teams_salary.index.names = ['employee_id']
    df_employee_roles_teams_salary.head()
    final_columns = ['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']
    
    #Merging DataFrames, reindexing and subsetting
    employees_final = df_employee_address.merge(df_emergency_contacts, how='left', on='employee_id') \
                       .merge(df_employee_roles_teams_salary, how='left', on='employee_id') \
                        .merge(df_office_addresses, how='left', left_on='employee_country', right_on='office_country').set_index(df_employee_address.index)[final_columns]
    
    
    #Filling NA values:
    office_columns = df_office_addresses.columns
    na_values = dict(list(zip(office_columns,['Remote']*len(office_columns))))
    employees_final.fillna(na_values, inplace=True)
    
    employees_final.head()
    #Verifying if the columns from employess_final match the final columns 
    assert all([True if column in employees_final.columns else False for column in final_columns])