Skip to content
Customer Analytics: Preparing Data for Modeling
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:
| Column | Description |
|---|---|
student_id | A unique ID for each student. |
city | A code for the city the student lives in. |
city_development_index | A scaled development index for the city. |
gender | The student's gender. |
relevant_experience | An indicator of the student's work relevant experience. |
enrolled_university | The type of university course enrolled in (if any). |
education_level | The student's education level. |
major_discipline | The educational discipline of the student. |
experience | The student's total work experience (in years). |
company_size | The number of employees at the student's current employer. |
company_type | The type of company employing the student. |
last_new_job | The number of years between the student's current and previous jobs. |
training_hours | The number of hours of training completed. |
job_change | An indicator of whether the student is looking for a new job (1) or not (0). |
Specifically, they have set the following requirements:
- Columns containing categories with only two factors must be stored as Booleans (bool).
- Columns containing integers only must be stored as 32-bit integers.
- Columns containing floats must be stored as 16-bit floats.
- Columns containing nominal categorical data must be stored as the category data type.
- Columns containing ordinal categorical data must be stored as ordered categories, and not mapped to numerical values, with an order that reflects the natural order of the column.
- The DataFrame should be filtered to only contain students with 10 or more years of experience at companies with at least 1000 employees, as their recruiter base is suited to more experienced professionals at enterprise companies.
import pandas as pd
from pandas.api.types import CategoricalDtype
ds_jobs = pd.read_csv("customer_train.csv")
ds_jobs.head()
ds_jobs_transformed = ds_jobs.copy()ds_jobs.info()# bool conversion
ds_jobs_transformed['relevant_experience'] = ds_jobs['relevant_experience'] == 'Has relevant experience'
ds_jobs_transformed['job_change'] = ds_jobs['job_change'] == 1.0
# int32 conversion
ds_jobs_transformed['student_id'] = ds_jobs['student_id'].astype('int32')
ds_jobs_transformed['training_hours'] = ds_jobs['training_hours'].astype('int32')
# float16 conversion
ds_jobs_transformed['city_development_index'] = ds_jobs['city_development_index'].astype('float16')
# define ordered categorical types
edu_order = ['Primary School', 'High School', 'Graduate', 'Masters', 'Phd']
enrollment_order = ['no_enrollment', 'Part time course', 'Full time course']
comp_size_order = ['<10', '10-49', '50-99', '100-499', '500-999',
'1000-4999', '5000-9999', '10000+']
experience_order = ['<1', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'10', '11', '12', '13', '14', '15', '16', '17',
'18', '19', '20', '>20']
last_job_order = ['never', '1 year', '2 years', '3 years', '4 years', '>4 years']
# apply ordered categoricals with a loop
ordered_columns = {
'education_level': CategoricalDtype(categories=edu_order, ordered=True),
'enrolled_university': CategoricalDtype(categories=enrollment_order, ordered=True),
'company_size': CategoricalDtype(categories=comp_size_order, ordered=True),
'experience': CategoricalDtype(categories=experience_order, ordered=True),
'last_new_job': CategoricalDtype(categories=last_job_order, ordered=True)
}
for col, dtype in ordered_columns.items():
ds_jobs_transformed[col] = ds_jobs[col].astype(dtype)
# apply nominal categoricals with a loop
nominal_columns = ['city', 'gender', 'major_discipline', 'company_type']
for col in nominal_columns:
ds_jobs_transformed[col] = ds_jobs[col].astype('category')
# filter the new df to include only 10+ years of experience and company size >= 1000 employees
valid_experience = experience_order[experience_order.index('10'):]
valid_sizes = comp_size_order[comp_size_order.index('1000-4999'):]
ds_jobs_transformed = ds_jobs_transformed[
ds_jobs_transformed['experience'].isin(valid_experience) &
ds_jobs_transformed['company_size'].isin(valid_sizes)]ds_jobs_transformed.info()orig_mem = ds_jobs.memory_usage().sum()
print(f"The original df has roughtly {orig_mem} bytes of memory.")trans_mem = ds_jobs_transformed.memory_usage().sum()
print(f"The revised df has roughtly {trans_mem} bytes of memory.")sweet_sweet_efficiency = round((trans_mem / orig_mem) * 100, 2)
reduction = round((1 - trans_mem / orig_mem) * 100)
print(f"Our data is prepped and we are now only using {sweet_sweet_efficiency}% of the initial dataframe size.")