Unit 3 Categorical - Data
Unit 3 Categorical - Data
Categorical data
Categorical variables can take on only a limited, and usually fixed number of possible values.
Besides the fixed length, categorical data might have an order but cannot perform numerical
operation. Categorical are a Pandas data type.
A string variable consisting of only a few different values. Converting such a string variable to a
categorical variable will save some memory.
The lexical order of a variable is not the same as the logical order (“one”, “two”, “three”). By
converting to a categorical and specifying an order on the categories, sorting and min/max will
use the logical order instead of the lexical order.
As a signal to other python libraries that this column should be treated as a categorical variable
(e.g. to use suitable statistical methods or plot types).
Object Creation
Categorical object can be created in multiple ways. The different ways have been described
below :
import pandas as pd
s = pd.Series(["a","b","c","a"], dtype="category")
print s
Its output is as follows,
0 a
1 b
2 c
3 a
dtype: category
Categories (3, object): [a, b, c]
The number of elements passed to the series object is four, but the categories are only three.
Observe the same in the output Categories.
pd.Categorical
Using the standard pandas Categorical constructor, we can create a category object.
pandas.Categorical(values, categories, ordered)
import pandas as pd
1
L.J. Institute of Engineering & Technology Semester: V (2022) PDS (3150713)
import pandas as pd
[a, b, c, a, b, c, NaN]
Logically, the order means that, a is greater than b and b is greater than c.
Describe()
Using the .describe() command on the categorical data, we get similar output to
a Series or DataFrame of the type string.
Describe() is used to view some basic statistical details like percentile, mean, std etc. of a data
frame or a series of numeric values.
import pandas as pd
import numpy as np
2
L.J. Institute of Engineering & Technology Semester: V (2022) PDS (3150713)
print df.describe()
print df["cat"].describe()
Renaming Categories
Renaming categories is done by assigning new values to the series.cat.categories property.
import pandas as pd
s = pd.Series(["a","b","c","a"], dtype="category")
s.cat.categories = ["Group %s" % g for g in s.cat.categories]
print s.cat.categories
Its output is as follows ,
Index([u'Group a', u'Group b', u'Group c'], dtype='object')
Initial categories [a,b,c] are updated by the s.cat.categories property of the object.
s = pd.Series(["a","b","c","a"], dtype="category")
s = s.cat.add_categories([4])
print s.cat.categories
Its output is as follows ,
3
L.J. Institute of Engineering & Technology Semester: V (2022) PDS (3150713)
Removing Categories
Using the Categorical.remove_categories() method, unwanted categories can be removed.
import pandas as pd
s = pd.Series(["a","b","c","a"], dtype="category")
print ("Original object:")
print s
After removal:
0 NaN
1 b
2 c
3 NaN
dtype: category
Categories (2, object): [b, c]
4
L.J. Institute of Engineering & Technology Semester: V (2022) PDS (3150713)
import pandas as pd
print cat>cat1
0 False
1 False
2 True
dtype: bool