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.
How to Create Frequency Tables in Python?
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.