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.

Weve 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.

We've been given access to customer_train.csv, which is a subset of their entire customer dataset, so we 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).

1) Converting columns with only two factors to Booleans (bool)

# Import necessary libraries
import pandas as pd

# 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 by checking the data type of each column
ds_jobs_transformed.info()
#check current values on job_change
ds_jobs_transformed["job_change"].value_counts(dropna=False)
#change data type on job_change column to boolean
ds_jobs_transformed["job_change"] = ds_jobs_transformed["job_change"].astype(bool)
print(ds_jobs_transformed["job_change"].head())
#check current values on the relevant_experience columns
ds_jobs_transformed["relevant_experience"].value_counts(dropna=False)
#create dictionary with replacement values for relevant_experience
replacement_dict = {"Has relevant experience": True, "No relevant experience":False }

#convert relevant_experience column to boolean data type
ds_jobs_transformed["relevant_experience"] = ds_jobs_transformed["relevant_experience"].replace(replacement_dict)

#confirm that the data type has change to boolean after value replacement
print(ds_jobs_transformed["relevant_experience"].head())

2) Converting columns with integers only to 32-bit integers (int32)

#change data types of columns student_id and training_hours from int to int32
ds_jobs_transformed[["student_id", "training_hours"]] = ds_jobs_transformed[["student_id", "training_hours"]].astype('int32')

#confirm changes
print(ds_jobs_transformed[["student_id", "training_hours"]].info())

3) Converting columns containing floats to 16-bit floats (float16)

#convert city_development_index column from float64 to float16 data type
ds_jobs_transformed["city_development_index"] = ds_jobs_transformed["city_development_index"].astype('float16')

#confirm changes
print(ds_jobs_transformed["city_development_index"].head())

4) Converting columns containing nominal category data to categories

#create list of columns with nominal categories and convert them to category data type
nomcat_cols = ["city","gender","major_discipline","company_type"]
ds_jobs_transformed[nomcat_cols] = ds_jobs_transformed[nomcat_cols].astype("category")

#confirm changes
print(ds_jobs_transformed[nomcat_cols].info())

5) Converting columns containing ordinal category data to ordered categories