How to Create Frequency Tables in Python?
Last Updated :
20 Feb, 2022
In this article, we are going to see how to Create Frequency Tables in Python
Frequency is a count of the number of occurrences a particular value occurs or appears in our data. A frequency table displays a set of values along with the frequency with which they appear. They allow us to better understand which data values are common and which are uncommon. These tables are a great method to arrange your data and communicate the results to others. In this article let's demonstrate the different ways in which we can create frequency tables in python.
To view and download the CSV file we use in this article click here.
Method 1: Simple frequency table using value_counts() method
Let's take a look at the dataset we'll work on :
The necessary packages are imported and the dataset is read using the pandas.read_csv() method. df.head() method returns the first 5 rows of the dataset.
Python3
# import packages
import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('iris.csv')
data.head()
Output:
Now let's find the one-way frequency table of the species column of the dataset.
Python3
df = data['species'].value_counts()
print(df)
Output:
setosa 50
virginica 50
versicolor 50
Name: species, dtype: int64
Method 2: One-way frequency table using pandas.crosstab() method
Here we are going to use crosstab() method to get the frequency.
Syntax: pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)
Parameters:
- index: array or series which contain values to group by in the rows.
- columns: array or series which contain values to group by in the columns.it's name we give to the column we find frequency
- values : An array of numbers that will be aggregated based on the factors.
In the below code we use the crosstab function where we give the species column as an index and 'no_of_species' as the name of the frequency column.
Python3
# import packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# reading csv file as pandas dataframe
data = pd.read_csv('iris.csv')
# one way frequency table for the species column.
freq_table = pd.crosstab(data['species'], 'no_of_species')
freq_table
Output: 50 plants belonging to the setosa species, 50 of Versicolor and 50 of Virginica.
If we want the frequency table to be in proportions then we've to divide each individual proportion by the sum of the total number.
Python3
# import packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# reading csv file as pandas dataframe
data = pd.read_csv('iris.csv')
# one way frequency table for the species column.
freq_table = pd.crosstab(data['species'], 'no_of_species')
# frequency table in proportion of species
freq_table= freq_table/len(data)
freq_table
Output: 0.333 indicates 0.333% of the total population is setosa and so on.

Method 3: Two-way frequency table using pandas.crosstab() method
Two - way frequency table is where we create a frequency table for two different features in our dataset. To download and review the CSV file used in this example click here. In the below example we create a two-way frequency table for the ship mode and segment columns of our dataset.
Python3
# import packages
import pandas as pd
import numpy as np
# reading csv file
data = pd.read_csv('SampleSuperstore.csv')
# two way frequency table for the ship mode column
# and consumer column of the superstore dataset.
freq_table = pd.crosstab(data['Ship Mode'], data['Segment'])
freq_table
Output:
We can interpret this table as for ship mode first class there are 769 consumer segments, 485 corporate segments and 284 home office segments, and so on.
Similar Reads
How to create Tables using Plotly in Python?
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
How to make a Table in Python?
Creating a table in Python involves structuring data into rows and columns for clear representation. Tables can be displayed in various formats, including plain text, grids or structured layouts. Python provides multiple ways to generate tables, depending on the complexity and data size.Using Tabula
3 min read
How to Create Frequency Table by Group using Dplyr in R
In this article, we will be looking at the approach to creating a frequency table group with its working examples in the R programming language. Create Frequency Table by Group using dplyr package: In this approach to create the frequency table by group, the user first needs to import and install th
2 min read
How to Make a Frequency Polygon in Excel?
Frequency is the number of occurrences of a repeating event per unit of time. A frequency Polygon is a graphical representation of the frequencies i.e., the distribution of data in a dataset. The frequency polygon became very useful in comparing multiple occurrences of distinct categories in a singl
2 min read
How to make a frequency distribution table in R ?
In this article, we are going to see how to make a frequency distribution table using R Programming Language. The table() method in R is used to compute the frequency counts of the variables appearing in the specified column of the dataframe. The result is returned to the form of a two-row tabular s
4 min read
How to Calculate Cumulative Frequency table in Excel?
Cumulative Frequency is the running total of frequencies starting from the very first frequency up to the last frequency. If we simplify the term running total, we can say the first frequency is added with the second frequency and then their sum is added to the third frequency and the same sequence
2 min read
How to count the frequency of unique values in NumPy array?
Let's see How to count the frequency of unique values in the NumPy array. Pythonâs Numpy library provides a numpy.unique() function to find the unique elements and their corresponding frequency in a NumPy array. numpy.unique() Syntax Syntax: numpy.unique(arr, return_counts=False) Return: Sorted uniq
4 min read
PostgreSQL - Create Tables in Python
Creating tables in PostgreSQL using Python is an essential skill for developers working with databases. This article will explore the process of creating new tables in the PostgreSQL database using Python.Why Create PostgreSQL Tables with Python?Using Python to create PostgreSQL tables is beneficial
4 min read
How to Perform an F-Test in Python
In statistics, Many tests are used to compare the different samples or groups and draw conclusions about populations. These techniques are commonly known as Statistical Tests or hypothesis Tests. It focuses on analyzing the likelihood or probability of obtaining the observed data that they are rando
10 min read
Find Frequency of Characters in Python
In this article, we will explore various methods to count the frequency of characters in a given string. One simple method to count frequency of each character is by using a dictionary.Using DictionaryThe idea is to traverse through each character in the string and keep a count of how many times it
2 min read