Using Label Encoding to Convert Categorical data to Numerical data

1. Loading the data

In [8]:
import pandas as pd
import numpy as np
In [2]:
banking_encode = pd.read_csv("Banking_Marketing.csv")
In [3]:
banking_encode.head()
Out[3]:
age job marital education default housing loan contact month day_of_week ... campaign pdays previous poutcome emp_var_rate cons_price_idx cons_conf_idx euribor3m nr_employed y
0 44.0 blue-collar married basic.4y unknown yes no cellular aug thu ... 1 999 0 nonexistent 1.4 93.444 -36.1 4.963 5228.1 0
1 53.0 technician married unknown no no no cellular nov fri ... 1 999 0 nonexistent -0.1 93.200 -42.0 4.021 5195.8 0
2 28.0 management single university.degree no yes no cellular jun thu ... 3 6 2 success -1.7 94.055 -39.8 0.729 4991.6 1
3 39.0 services married high.school no no no cellular apr fri ... 2 999 0 nonexistent -1.8 93.075 -47.1 1.405 5099.1 0
4 55.0 retired married basic.4y no yes no cellular aug fri ... 1 3 1 success -2.9 92.201 -31.4 0.869 5076.2 1

5 rows × 21 columns

2. Finding the total NA values across columns

In [4]:
banking_encode.isna().sum()
Out[4]:
age               2
job               0
marital           0
education         0
default           0
housing           0
loan              0
contact           6
month             0
day_of_week       0
duration          7
campaign          0
pdays             0
previous          0
poutcome          0
emp_var_rate      0
cons_price_idx    0
cons_conf_idx     0
euribor3m         0
nr_employed       0
y                 0
dtype: int64

3. Removing the NAs from the different columns

In [5]:
banking_encode = banking_encode.dropna()

4. No NA values after dropping the NAs in the above step

In [6]:
banking_encode.isna().sum()
Out[6]:
age               0
job               0
marital           0
education         0
default           0
housing           0
loan              0
contact           0
month             0
day_of_week       0
duration          0
campaign          0
pdays             0
previous          0
poutcome          0
emp_var_rate      0
cons_price_idx    0
cons_conf_idx     0
euribor3m         0
nr_employed       0
y                 0
dtype: int64

5. Excluding the Numerical columns from the dataframe so that we could encode only the categorical columns

In [9]:
banking_exclude_numeric = banking_encode.select_dtypes(exclude=np.number).columns

6. The Categorical columns are below

In [10]:
banking_encode[banking_exclude_numeric].head()
Out[10]:
job marital education default housing loan contact month day_of_week poutcome
0 blue-collar married basic.4y unknown yes no cellular aug thu nonexistent
1 technician married unknown no no no cellular nov fri nonexistent
2 management single university.degree no yes no cellular jun thu success
3 services married high.school no no no cellular apr fri nonexistent
4 retired married basic.4y no yes no cellular aug fri success
In [11]:
banking_exclude_numeric
Out[11]:
Index(['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact',
       'month', 'day_of_week', 'poutcome'],
      dtype='object')

7. Looping through the "banking_exckude_numeric" to encode each categorical columns

In [12]:
from sklearn.preprocessing import LabelEncoder
In [13]:
label_encoder = LabelEncoder()
In [14]:
for i in banking_exclude_numeric:
    banking_encode[i] = label_encoder.fit_transform(banking_encode[i])
print("Encoded data")
banking_encode.head()
Encoded data
Out[14]:
age job marital education default housing loan contact month day_of_week ... campaign pdays previous poutcome emp_var_rate cons_price_idx cons_conf_idx euribor3m nr_employed y
0 44.0 1 1 0 1 2 0 0 1 2 ... 1 999 0 1 1.4 93.444 -36.1 4.963 5228.1 0
1 53.0 9 1 7 0 0 0 0 7 0 ... 1 999 0 1 -0.1 93.200 -42.0 4.021 5195.8 0
2 28.0 4 2 6 0 2 0 0 4 2 ... 3 6 2 2 -1.7 94.055 -39.8 0.729 4991.6 1
3 39.0 7 1 3 0 0 0 0 0 0 ... 2 999 0 1 -1.8 93.075 -47.1 1.405 5099.1 0
4 55.0 5 1 0 0 2 0 0 1 0 ... 1 3 1 2 -2.9 92.201 -31.4 0.869 5076.2 1

5 rows × 21 columns

Above,you can see that all the values have been converted from categorical to numerical. Here, the original values have been transformed and replaced with the newly encoded data. You have successfully converted categorical data to numerical data using the LabelEncoder method.