Replacement of Categorical data with a Number

1. Importing the dataset

In [1]:
import numpy as np
import pandas as pd
In [2]:
dataset_student = pd.read_csv("student.csv",header = 0)
In [3]:
dataset_student.head()
Out[3]:
Student_id Age Gender Grade Employed
0 1 19 Male 1st Class yes
1 2 20 Female 2nd Class no
2 3 18 Male 1st Class no
3 4 21 Female 2nd Class no
4 5 19 Male 1st Class no

2. Excluding the numerical columns and only retrieving the categorical columns here

In [4]:
categorical_df = dataset_student.select_dtypes(exclude=[np.number])
In [5]:
categorical_df.head()
Out[5]:
Gender Grade Employed
0 Male 1st Class yes
1 Female 2nd Class no
2 Male 1st Class no
3 Female 2nd Class no
4 Male 1st Class no

3. Checking the unique columns before Replacing them with a number

In [6]:
categorical_df['Grade'].unique()
Out[6]:
array(['1st Class', '2nd Class', '3rd Class'], dtype=object)

4. Count of individual grades in the Grade column

In [7]:
categorical_df.Grade.value_counts()
Out[7]:
3rd Class    80
2nd Class    80
1st Class    72
Name: Grade, dtype: int64

5. Count of the Gender column

In [8]:
categorical_df.Gender.value_counts()
Out[8]:
Male      136
Female     96
Name: Gender, dtype: int64

6. Count of the Employed column

In [9]:
categorical_df.Employed.value_counts()
Out[9]:
no     133
yes     99
Name: Employed, dtype: int64

7. Replacing the categorical values of Grade column with number

In [10]:
categorical_df.Grade.replace({"1st Class":1,"2nd Class":2,"3rd Class":3}).head()
Out[10]:
0    1
1    2
2    1
3    2
4    1
Name: Grade, dtype: int64

8.Replacing the categorical values of Gender column with number

In [11]:
categorical_df.Gender.replace({"Male":0,
                              "Female":1}).head()
Out[11]:
0    0
1    1
2    0
3    1
4    0
Name: Gender, dtype: int64

9.Replacing the categorical values of Employed column with number

In [12]:
categorical_df.Employed.replace({"yes":1,
                                "no":0}).head()
Out[12]:
0    1
1    0
2    0
3    0
4    0
Name: Employed, dtype: int64
In [13]:
categorical_df.head()
Out[13]:
Gender Grade Employed
0 Male 1st Class yes
1 Female 2nd Class no
2 Male 1st Class no
3 Female 2nd Class no
4 Male 1st Class no

Using inplace=True

1.Load the dataset

In [14]:
import numpy as np
import pandas as pd

2. header = 0 specifies that the header of the DataFrame should be considered from the index 0

In [15]:
dataset_student = pd.read_csv("student.csv",header = 0)
In [16]:
dataset_student.head()
Out[16]:
Student_id Age Gender Grade Employed
0 1 19 Male 1st Class yes
1 2 20 Female 2nd Class no
2 3 18 Male 1st Class no
3 4 21 Female 2nd Class no
4 5 19 Male 1st Class no
In [17]:
categorical_df = dataset_student.select_dtypes(exclude=[np.number])

3. Using inplace = True makes sure that the changes are reflected to the DataFrame after changes are applied. By default it is set to inplace = False

In [18]:
categorical_df.Grade.replace({"1st Class":1,"2nd Class":2,"3rd Class":3},inplace=True)
C:\Users\tkhan050\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py:6786: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._update_inplace(new_data)
In [19]:
categorical_df.Gender.replace({"Male":0,
                              "Female":1},inplace=True)
In [20]:
categorical_df.Employed.replace({"yes":1,
                                "no":0},inplace = True)
In [21]:
categorical_df.head()
Out[21]:
Gender Grade Employed
0 0 1 1
1 1 2 0
2 0 1 0
3 1 2 0
4 0 1 0