Categorical variables
Tags: statistics, datascience, python, beginners
Published: 2020-07-07 21:26:05

A categorical variable (sometimes called a nominal variable) is a variable that can assume one of a limited number of possible values described as categories and there is no intrinsic ordering to the categories. It uses labels, names, or other descriptors (even numbers) to identify *exclusive* categories or types of things.

As an example of a categorical variable, we may mention **Nationality** having values like *Brazilian, Canadian, French, etc.*, and we can see that there is no ordering between the values: we cannot say that Brazilian is higher than Canadian. In summary, there is no way to order these categories from highest to lowest or from best to worst.

Other examples of categorical variables could be **Regions** (*North, South, East, West*), **Blood Type** (*A, B, AB, O*) or **Smartphone Brand** (*Apple, Samsumg, LG, Xiami*). 

However, if there is a clear order between the categories, so we are dealing with an ordinal variable, that is very similar to a categorical variable and often it's considered a special kind of this and placed on between categorical and quantitative variables. An example of an ordinal variable could be Educational Level (*Elementary school education, High school graduate, Some college, College graduate, Graduate degree*).

But in this article we are focusing on pure categorical or nominal variables, so let's check out what we can do with some categorical data.

## Frequency distribution

Since we have a dataset with some categorical variables, the most common thing we can do is count the occurrences of each category in the whole data. This will give us a frequency distribution.

Let's take a look at some real data to demonstrate a frequency distribution. We will use the Kaggle [Google Play Store Apps](https://www.kaggle.com/lava18/google-play-store-apps) dataset from [Lavanya Gupta](https://www.kaggle.com/lava18). This dataset has more than 10,000 rows, each of them is an app from Google Play Store, and as features (columns) we can see the App name, Category, Rating, and others.

We will use pandas for handling the data. Firstly, we import pandas and read the CSV file downloaded from Kaggle, but only the *Category* column. Then, we use the `unique` method to show all values observed in our data. As we can see, there are 34 App Categories in our categorical variable, like *Finance, Sports, Weathers* and others and we can't see any order between them (*Events* category is not better or higher than *Shopping* category, for instance).


```python
import pandas as pd

df = pd.read_csv("./data/googleplaystore.csv", usecols=['Category'])
categories = df['Category'].unique()

print(f"{len(categories)} categories:")
print(categories)
```

    34 categories:
    ['ART_AND_DESIGN' 'AUTO_AND_VEHICLES' 'BEAUTY' 'BOOKS_AND_REFERENCE'
     'BUSINESS' 'COMICS' 'COMMUNICATION' 'DATING' 'EDUCATION' 'ENTERTAINMENT'
     'EVENTS' 'FINANCE' 'FOOD_AND_DRINK' 'HEALTH_AND_FITNESS' 'HOUSE_AND_HOME'
     'LIBRARIES_AND_DEMO' 'LIFESTYLE' 'GAME' 'FAMILY' 'MEDICAL' 'SOCIAL'
     'SHOPPING' 'PHOTOGRAPHY' 'SPORTS' 'TRAVEL_AND_LOCAL' 'TOOLS'
     'PERSONALIZATION' 'PRODUCTIVITY' 'PARENTING' 'WEATHER' 'VIDEO_PLAYERS'
     'NEWS_AND_MAGAZINES' 'MAPS_AND_NAVIGATION' '1.9']


Now that we know all category values we can have, let's count how many times every category occurs in our data using `value_counts` method.


```python
frequency = df['Category'].value_counts()

# frequency is a pandas Series, so we'll transform it in a DataFrame just for presentation purposes
frequency_dist = pd.DataFrame(frequency)
frequency_dist.columns = ['Frequency']
frequency_dist.index.name = 'Category'

# Using head(10) to show only the first 10 lines
frequency_dist.head(10)
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Frequency</th>
    </tr>
    <tr>
      <th>Category</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>FAMILY</th>
      <td>1972</td>
    </tr>
    <tr>
      <th>GAME</th>
      <td>1144</td>
    </tr>
    <tr>
      <th>TOOLS</th>
      <td>843</td>
    </tr>
    <tr>
      <th>MEDICAL</th>
      <td>463</td>
    </tr>
    <tr>
      <th>BUSINESS</th>
      <td>460</td>
    </tr>
    <tr>
      <th>PRODUCTIVITY</th>
      <td>424</td>
    </tr>
    <tr>
      <th>PERSONALIZATION</th>
      <td>392</td>
    </tr>
    <tr>
      <th>COMMUNICATION</th>
      <td>387</td>
    </tr>
    <tr>
      <th>SPORTS</th>
      <td>384</td>
    </tr>
    <tr>
      <th>LIFESTYLE</th>
      <td>382</td>
    </tr>
  </tbody>
</table>
</div>



So, we can see above that most apps are from the *Family* category with 1,972 occurrences. *Game* and *Tools* are also common categories, on the other hand, there are few apps from the *Beauty* category.

## Relative Frequency

At the moment we already know how many apps we have from each category. But what if we wanted to figure out what is the percentage of Medical apps of all apps? Then we need to calculate the relative frequency of category apps dividing the frequency by the total number of apps (aka the sample data).

Relative frequency of something = Frequency of something / n

Again, we will use the marvelous pandas. The relative frequency must assume a value from 0 to 1, but here we will multiply it by 100 and show the values in percentage form instead. So, as you can see below, Medical apps represent approximately 4.27% of all apps in Google Play Store according to our dataset.


```python
frequency_dist['Relative Frequency (%)'] = (frequency_dist['Frequency']/sum(frequency_dist['Frequency']))*100

# Using head(10) to show only the first 10 lines
frequency_dist.head(10)
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Frequency</th>
      <th>Relative Frequency (%)</th>
    </tr>
    <tr>
      <th>Category</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>FAMILY</th>
      <td>1972</td>
      <td>18.190204</td>
    </tr>
    <tr>
      <th>GAME</th>
      <td>1144</td>
      <td>10.552532</td>
    </tr>
    <tr>
      <th>TOOLS</th>
      <td>843</td>
      <td>7.776035</td>
    </tr>
    <tr>
      <th>MEDICAL</th>
      <td>463</td>
      <td>4.270824</td>
    </tr>
    <tr>
      <th>BUSINESS</th>
      <td>460</td>
      <td>4.243151</td>
    </tr>
    <tr>
      <th>PRODUCTIVITY</th>
      <td>424</td>
      <td>3.911078</td>
    </tr>
    <tr>
      <th>PERSONALIZATION</th>
      <td>392</td>
      <td>3.615903</td>
    </tr>
    <tr>
      <th>COMMUNICATION</th>
      <td>387</td>
      <td>3.569781</td>
    </tr>
    <tr>
      <th>SPORTS</th>
      <td>384</td>
      <td>3.542109</td>
    </tr>
    <tr>
      <th>LIFESTYLE</th>
      <td>382</td>
      <td>3.523660</td>
    </tr>
  </tbody>
</table>
</div>



## Frequency Bar Chart

Finally, we will plot the frequency variable in a Bar Chart that is a pretty common way to visualize categorical data. 


```python
import plotly.express as px

fig = px.bar(frequency)
fig.update_layout(title='Frequency Distribution of Google Play Store app categories',
                  xaxis_title='Category',
                  yaxis_title='Frequency')
fig.show()
```


![Bar Chart](https://dev-to-uploads.s3.amazonaws.com/i/p7z80wxxmbr72iub40bn.png)

So, in this article we have seen a bit about Categorical Variables or Nominal Variables, which is a pretty usual data type we face in Statistics, Data Analysis, Machine Learning, and so on. It was just an introductory content, but we may cover it a little deeper in upcoming posts.

## References

Wikipedia | Categorical variable [🔎](https://en.wikipedia.org/wiki/Categorical_variable)
UCLA | WHAT IS THE DIFFERENCE BETWEEN CATEGORICAL, ORDINAL AND NUMERICAL VARIABLES? [🔎](https://stats.idre.ucla.edu/other/mult-pkg/whatstat/what-is-the-difference-between-categorical-ordinal-and-numerical-variables/)
Brandon Foltz | Statistics 101: Describing a Categorical Variable
 [🔎](https://www.youtube.com/watch?v=vrWYw8d2830)
web.ma.utexas.edu | Ordinal Variables [🔎](https://web.ma.utexas.edu/users/mks/statmistakes/ordinal.html)