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:
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. |
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 ). |
# Start your code here!
import pandas as pd
ds_jobs = pd.read_csv('customer_train.csv')
# Exploring the ds_jobs dataset
#print(ds_jobs.head())
#print(ds_jobs.columns)
#print(ds_jobs.info())
#print(ds_jobs.memory_usage())
# Converting dtpes for columns containing integers and floats
ds_jobs['student_id'] = ds_jobs['student_id'].astype('int32')
ds_jobs['city_development_index'] = ds_jobs['city_development_index'].astype('float16')
ds_jobs['training_hours'] = ds_jobs['training_hours'].astype('int32')
ds_jobs['job_change'] = ds_jobs['job_change'].astype('int32')
# Exploring the ds_jobs dataset
#print(ds_jobs.head())
#print(ds_jobs.columns)
#print(ds_jobs.info())
#print(ds_jobs[['city','gender','relevant_experience', 'enrolled_university', 'education_level', 'major_discipline', 'experience', 'company_size', 'company_type', 'last_new_job']]) # subsets the columns of ds_jobs that have an object data type
# Converting the object dtypes to categorical
ds_jobs['city'] = ds_jobs['city'].astype('category')
ds_jobs['gender'] = ds_jobs['gender'].astype('category')
ds_jobs['relevant_experience'] = ds_jobs['relevant_experience'].astype('category')
ds_jobs['enrolled_university'] = ds_jobs['enrolled_university'].astype('category')
ds_jobs['education_level'] = ds_jobs['education_level'].astype('category')
ds_jobs['major_discipline'] = ds_jobs['major_discipline'].astype('category')
ds_jobs['experience'] = ds_jobs['experience'].astype('category')
ds_jobs['company_size'] = ds_jobs['company_size'].astype('category')
ds_jobs['company_type'] = ds_jobs['company_type'].astype('category')
ds_jobs['last_new_job'] = ds_jobs['last_new_job'].astype('category')
# Setting the categories for the ordinal categorical columns
#print(ds_jobs['city'].value_counts())
#print(ds_jobs['city'].describe())
#print(ds_jobs['gender'].value_counts())
ds_jobs['relevant_experience'].cat.set_categories(new_categories = ['No relevant experience', 'Has relevant experience'], ordered = True, inplace = True)
ds_jobs['enrolled_university'].cat.set_categories(new_categories = ['no_enrollment', 'Part time course', 'Full time course'], ordered = True, inplace=True)
ds_jobs['education_level'].cat.set_categories(new_categories = ['Primary School', 'High School', 'Graduate', 'Masters', 'Phd'], ordered = True, inplace=True)
#print(ds_jobs['major_discipline'].value_counts())
#print(ds_jobs['experience'].value_counts())
ds_jobs['experience'].cat.set_categories(new_categories = ['NaN', '<1', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '>20'], ordered = True, inplace=True)
#print(ds_jobs['company_size'].value_counts())
ds_jobs['company_size'].cat.set_categories(new_categories = ['<10', '10-49', '50-99', '100-499', '500-999', '1000-4999', '5000-9999', '10000+'], ordered = True, inplace=True)
#print(ds_jobs['company_type'].value_counts())
ds_jobs['last_new_job'].cat.set_categories(new_categories = ['never', '1', '2', '3', '4', '>4'], ordered = True, inplace=True)
#print(ds_jobs['last_new_job'].value_counts())
# Converting the columns back to the categorical dtype
ds_jobs['city'] = ds_jobs['city'].astype('category')
ds_jobs['gender'] = ds_jobs['gender'].astype('category')
ds_jobs['relevant_experience'] = ds_jobs['relevant_experience'].astype('category')
ds_jobs['enrolled_university'] = ds_jobs['enrolled_university'].astype('category')
ds_jobs['education_level'] = ds_jobs['education_level'].astype('category')
ds_jobs['major_discipline'] = ds_jobs['major_discipline'].astype('category')
ds_jobs['experience'] = ds_jobs['experience'].astype('category')
ds_jobs['company_size'] = ds_jobs['company_size'].astype('category')
ds_jobs['company_type'] = ds_jobs['company_type'].astype('category')
ds_jobs['last_new_job'] = ds_jobs['last_new_job'].astype('category')
# Converting dtpes for columns containing integers and floats again
ds_jobs['student_id'] = ds_jobs['student_id'].astype('int32')
ds_jobs['city_development_index'] = ds_jobs['city_development_index'].astype('float16')
ds_jobs['training_hours'] = ds_jobs['training_hours'].astype('int32')
ds_jobs['job_change'] = ds_jobs['job_change'].astype('int32')
# Cross checking the ds_jobs dataset again
#print(ds_jobs.head())
#print(ds_jobs.columns)
#print(ds_jobs.info())
#print(ds_jobs[['city','gender','relevant_experience', 'enrolled_university', 'education_level', 'major_discipline', 'experience', 'company_size', 'company_type', 'last_new_job']].head()) # subsets the columns of ds_jobs that have an object data type
ds_jobs_clean = ds_jobs[(ds_jobs['experience'] > '9') & (ds_jobs['company_size'] >= '1000-4999')] # filters for rows where experience is at least 10 years AND company size is equal to at least 1000 employees and saves it all as a new dataset named ds_jobs_clean
# Examining the ds_jobs_clean dataset
#print(ds_jobs_clean[['experience', 'company_size']].value_counts())
#print(ds_jobs_clean.groupby(by = ['experience'])['company_size'].value_counts())
#print(ds_jobs_clean.groupby(by = ['company_size'])['experience'].value_counts())
#print(ds_jobs_clean.dtypes) # displays the data types of the columns of ds_jobs_clean
#print(ds_jobs_clean.info())
#print(ds_jobs_clean.memory_usage())
#print(ds_jobs_clean[['city', 'education_level', 'experience', 'company_size', 'last_new_job']].head())