How to print Frequency Table for all categorical variables using value_counts() function?

Home » News » How to print Frequency Table for all categorical variables using value_counts() function?
Frequency Table displays the frequency of occurrence of each and every category in a feature. This provides a very useful information while analyzing the categorical variables. Pandas library provides value_counts function for this.


Consider a Load Prediction dataset. We will try find out frequency of occurrence of each and every category in all the variables.

Step 1: Import the required libraries

import pandas as pd
import numpy as np

Step 2: Load the dataset

dataset = pd.read_csv(“C:/train_loan_prediction.csv”)


Step 3: Find datatype of all variables


dataset.info()
dataset.dtypes


We find that columns Loan_ID, Gender, Married, Dependents, Education, Self_Employed are of object type (categorical variables).


Step 4: Draw Frequency Table


#Find all the categorical variables
categorical_columns = [x for x in dataset.dtypes.index if dataset.dtypes[x]=='object']


#Exclude Load_ID column
categorical_columns = [x for x in categorical_columns if x not in [‘Loan_ID']]


#Print frequency of categories
for col in categorical_columns:
    print(‘nFrequency of Categories for variable %s'%col)
    print(dataset[col].value_counts())

Run the above code and observe the results. It displays frequency of all the categorical variables (how many times a particular category exists in a feature).


SimilarHow to use pandas value_counts() function to impute missing values?

Leave a Reply

Your email address will not be published. Required fields are marked *

New Providers
Binolla

The Broker
More then 2 million businesses
See Top 10 Broker

gamehag

Online game
More then 2 million businesses
See Top 10 Free Online Games

New Games
Lies of P

$59.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

COCOON

$24.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

New Offers
Commission up to $1850 for active user of affiliate program By Exness

Top Points © Copyright 2023 | By Topoin.com Media LLC.
Topoin.info is a site for reviewing the best and most trusted products, bonus, offers, business service providers and companies of all time.

Discover more from Top Points

Subscribe now to keep reading and get access to the full archive.

Continue reading