Skip to content

A common problem when creating models to generate business value from data is that the datasets can be so large that it can take days for the model to generate predictions. Ensuring that your dataset is stored as efficiently as possible is crucial for allowing these models to run on a more reasonable timescale without having to reduce the size of the dataset.

You've been hired by a major online data science training provider called Training Data Ltd. to clean up one of their largest customer datasets. This dataset will eventually be used to predict whether their students are looking for a new job or not, information that they will then use to direct them to prospective recruiters.

You've been given access to customer_train.csv, which is a subset of their entire customer dataset, so you can create a proof-of-concept of a much more efficient storage solution. The dataset contains anonymized student information, and whether they were looking for a new job or not during training:

ColumnDescription
student_idA unique ID for each student.
cityA code for the city the student lives in.
city_development_indexA scaled development index for the city.
genderThe student's gender.
relevant_experienceAn indicator of the student's work relevant experience.
enrolled_universityThe type of university course enrolled in (if any).
education_levelThe student's education level.
major_disciplineThe educational discipline of the student.
experienceThe student's total work experience (in years).
company_sizeThe number of employees at the student's current employer.
company_typeThe type of company employing the student.
last_new_jobThe number of years between the student's current and previous jobs.
training_hoursThe number of hours of training completed.
job_changeAn indicator of whether the student is looking for a new job (1) or not (0).
# Import necessary libraries
import pandas as pd
from pandas.api.types import CategoricalDtype

# Load the dataset
ds_jobs = pd.read_csv("customer_train.csv")

# View the dataset
ds_jobs.head()
# Create a copy of ds_jobs for transforming
ds_jobs_transformed = ds_jobs.copy()

# Start coding here. Use as many cells as you like!
ds_jobs_transformed.info()
print(ds_jobs_transformed.experience.value_counts())
ds_jobs_transformed.enrolled_university.value_counts()
ds_jobs_transformed.education_level.value_counts()
ds_jobs_transformed.last_new_job.value_counts()
# Define the order of categories for various categorical columns
job_change = ['never', '1', '2', '3', '4', '>4']
education = ['Primary School', 'High School', 'Graduate', 'Masters', 'Phd']
course = ['no_enrollment', 'Full time course', 'Part time course']
years = ['<1'] + list(map(str, range(1, 21))) + ['>20']
size = ['<10', '10-49', '50-99', '100-499', '500-999', '1000-4999', '5000-9999', '10000+']

# Create CategoricalDtype objects with the specified order
education_order = CategoricalDtype(categories=education, ordered=True)
course_order = CategoricalDtype(categories=course, ordered=True)
years_order = CategoricalDtype(categories=years, ordered=True)
size_order = CategoricalDtype(categories=size, ordered=True)
job_change_order = CategoricalDtype(categories=job_change, ordered=True)

# Define a dictionary to map columns to their new data types
new_dtypes = {
    'last_new_job': job_change_order,  # Ordered categorical type for 'last_new_job'
    'student_id': 'int32',  # Integer type for 'student_id'
    'city': 'category',  # Categorical type for 'city'
    'city_development_index': 'float16',  # Float type with reduced precision for 'city_development_index'
    'company_type': 'category',  # Categorical type for 'company_type'
    'enrolled_university': course_order,  # Ordered categorical type for 'enrolled_university'
    'gender': 'category',  # Categorical type for 'gender'
    'relevant_experience': 'bool',  # Boolean type for 'relevant_experience'
    'education_level': education_order,  # Ordered categorical type for 'education_level'
    'major_discipline': 'category',  # Categorical type for 'major_discipline'
    'training_hours': 'int32',  # Integer type for 'training_hours'
    'job_change': 'bool',  # Boolean type for 'job_change'
    'company_size': size_order,  # Ordered categorical type for 'company_size'
    'experience': years_order  # Ordered categorical type for 'experience'
}


# Display the dataframe information to verify changes
ds_jobs_transformed.info()
ds_jobs_transformed['experience'].value_counts()
print(ds_jobs_transformed['company_size'].value_counts())
# The code filters the DataFrame `ds_jobs_transformed` to include only rows where:
# 1. The 'experience' column has values greater than or equal to '10'.
# 2. The 'company_size' column has values greater than or equal to '1000-4999'.
# The filtered DataFrame is then assigned back to `ds_jobs_transformed`.

# Note: This code may not work as intended because 'experience' and 'company_size' are categorical variables.
# Comparisons like '>=' may not behave as expected with categorical data.
# Additionally, 'experience' values like '10' might be strings, not integers.

# Display the filtered DataFrame
ds_jobs_transformed = ds_jobs_transformed[
    (ds_jobs_transformed['experience'] >= '10') &
    (ds_jobs_transformed['company_size'] >= '1000-4999')
]

ds_jobs_transformed