Python Tutorial: Transforming categorical variables
Skills:
Python for Data80%
Key Takeaways
This video tutorial by DataCamp covers transforming categorical variables into numerical variables in Python, specifically ordinal and nominal variables, using methods such as encoding categories and creating dummy variables.
Full Transcript
now that we know what are the categorical variables in our dataset we can start transforming them into numerical to transform a categorical variable into numeric we have to understand its type first there are two types of categorical variables ordinal and nominal ordinal variables have two or more categories that can be ranked or ordered in our case there's the salary column or the values clearly have a logical order the second type is nominal where categories do not have any intrinsic or logical order an example of this kind of rebel in our dataset is the count Department as its values clearly do not have any order or rank sales department is not higher than ager or vice versa and so on based on what type of categorical variable you have there are different methods for transforming them for the case of ordinal variables we can encode categories by converting each of them into respective numeric value the three steps to accomplish this task in Python first we have to tell Python that the counsellor is actually categorical this is done by using method called test type which is providing the type of the variable then one spy to note that it's a categorical variable we have to tell the correct order of categories using carry order categories method as you can see in the code this method takes the list as an input where the correct order of categories is provided last but not least we have to use cat codes attributes to encode each category with a numeric value given our order the result will override the old values of salary column the new numeric values as presented in a table the next categorical variable is nominal and there is no order of rank between departments this means that the encoding approach is not useful anymore in this case transformation should be accomplished through so called dummy variables dummy variables are the variables that can only get two values 0 or 1 let's an employee is from the technical department this means if we have a separate column for each department then the mentioned employee will have value of 1 in that column for technical and 0 in all other columns of departments this mess we will have to create a new data frame where each department is a separate column and each row is a separate employee with wounds in front of his/her department and zero in all other places well the task seems to be confusing it's very easy from technical perspective do two very nice function from pandas called gilt dummies well the minute dummy variables one should be cautious of a phenomenon known as dummy trap the latter is the situation when different dummy rebels come with the same information in this example the sample employees from the technical department so it's the only column with a value of 1 in the first table in the second table the last column is dropped but we can still understand that the employees from technical department by looking at all the other departments that have value of zero for that reason whenever in similar situations dummies are created one of them can be dropped as its information is already included in others okay time to put this
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/human-resources-analytics-predicting-employee-churn-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Now that we know what are the categorical variables in our dataset we can start transforming them into numerical.
To transform a categorical variable into numeric, we have to understand it's type first. There are two types of categorical variables: ordinal and nominal. Ordinal variables have two or more categories that can be ranked or ordered. In our case that is the **salary** column, where the values clearly have a logical order.
The 2nd type is Nominal, where categories do not have any intrnisic or logical order. An example of this kind of variable in our dataset is the column **department**, as its values clearly do not have any order or rank: sales department is not higher than hr or viceversa and so on.
Based on what type of categorical variable you have, there are different methods for transforming them.
For the case of ordinal variables we can encode categories by converting each of them into a respective numeric value. There are 3 steps to accomplish that tasks in Python.
- First, we have to tell Python, that the column salary is actually categorical. This is done using a method called **astype()** which is providing the type of the variable.
- Then, once Python knows that it is a categorical variable, we have to tell the correct order of categories, using cat.reorder_categories() method. As you can see in the code, this method takes a list as an input, where the correct order of categories is provided.
- Last but not least, we have to use cat.codes attribute to encode each category with a numeric value given our order. The result will overwrite the old values of salary column with new numeric values as presented in the table.
The next categorical variable is nominal, as there is no order or rank bet
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI