Login
📚 Python for Introductory Statistics
Chapters ▾

5.1 Frequency Distribution

For a unique count of each data value in a dataset use the syntax:

import numpy as np
x = [x1,x2,x3,...]
np.unique(x,return_counts=True)

Note: Dataset will be entered in Python list as x = [x1,x2,x3,...]

Example: The dataset below is the waiting time, in minutes, of customers at a fast food restaurant. Write a program to construct a frequency of unique values.

5, 3, 4, 1, 5, 3, 5, 2, 3, 2, 5, 4, 4, 6, 4, 3, 4, 1, 2, 5

Watch demo video

  1. Click + Code
  2. Type import numpy as np
  3. Type x = [5, 3, 4, 1, 5, 3, 5, 2, 3, 2, 5, 4, 4, 6, 4, 3, 4, 1, 2, 5]
  4. Type np.unique(x,return_counts=True)
  5. Click play button
  6. Two array output is displayed - unique value and frequency
import numpy as np
x = [5, 3, 4, 1, 5, 3, 5, 2, 3, 2, 5, 4, 4, 6, 4, 3, 4, 1, 2, 5]
np.unique(x,return_counts=True)
Show expected output
(array([1, 2, 3, 4, 5, 6]), array([2, 3, 4, 5, 5, 1]))

Interpretation: In the above output, the first array is the list of unique elements and the second array is counts (frequencies) for each unique element. For example, the value 1 from the first array has a frequency of 2 from the second array, etc.

valuefrequency
12
23
34
45
55
61

Note: We will use the variable name x to store the data values in a Python List.

Example: 20 students were asked if they were registered to vote. Their responses are listed below. Write a program to construct a frequency table.

NO, NO, YES, YES, NO, YES, NO, NO, YES, NO, YES, NO, NO, YES, YES, YES, YES, NO, YES, YES

Watch demo video

import numpy as np
x = ['NO', 'NO', 'YES', 'YES', 'NO', 'YES', 'NO', 'NO', 'YES', 'NO', 
             'YES', 'NO', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES']
np.unique(x,return_counts=True)
Show expected output
(array(['NO', 'YES'], dtype='<U3'), array([ 9, 11]))

Interpretation: from the above output:

valuefrequency
NO9
YES11

Note: Qualitative data values are entered in 'quotes'. The dtype “<U3” refers to a Unicode string of 3 characters.

For making a frequency distribution with k classes (bins), use the following code:

import numpy as np
x = [x1,x2,x3,...]
np.histogram(x,bins=k,density=False)

If density=True the frequencies are replaced by relative frequencies.

Example: The dataset below is test scores of 20 students in a Math class. Write a program to construct a frequency distribution with 5 classes(bins).

33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100

Watch demo video

  1. Click + Code
  2. Type import numpy as np
  3. Type x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74, 78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100]
  4. np.histogram(x, bins=5,density=False)
  5. Two array output - frequencies and classes
import numpy as np
x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,
             78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100]
np.histogram(x, bins=5,density=False)
Show expected output
(array([ 2,  5,  9,  4, 11]),
 array([ 33. ,  46.4,  59.8,  73.2,  86.6, 100. ]))

Interpretation from the above output:

classesfreq
33 - 46.42
46.4 - 59.85
59.8 - 73.29
73.2 - 86.64
86.6 - 10011

key: 33-46.4 is [33,46.4) or 33x<46.4

Example: The dataset below is test scores of 20 students in a Math class. Write a program to construct a frequency distribution with 5 classes(bins). Save the two arrays as freq and class_limits

33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100

  1. Click + Code
  2. Type import numpy as np
  3. Type x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74, 78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100]
  4. Type freq, class_limit = np.histogram(x, bins=5,density=False)
  5. Type print(class_limit)
  6. Type print(freq)
import numpy as np
x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,
             78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100]
freq, class_limit = np.histogram(x, bins=5,density=False)
print(class_limit)
print(freq)
Show expected output
[ 33.   46.4  59.8  73.2  86.6 100. ]
[ 2  5  9  4 11]

Interpretation from the above output:

classesfreq
33 - 46.42
46.4 - 59.85
59.8 - 73.29
73.2 - 86.64
86.6 - 10011

key: 33-46.4 is [33,46.4) or 33x<46.4

Example: Use the US Senators age (as of 2021) dataset given below to construct a relative frequency distribution with 5 bins (classes).

81,81,80,80,80,80,78,78,78,77,77,75,75,74,74,74,73,72,72,71,71,71,71,70,70,70,70,68,68,68,68,68,67,67,67,66,66,65,65,65,65,64,64,64,64,64,64,63,63,63,63,62,62,62,62,62,61,61,61,60,60,59,59,59,59,59,59,59,59,58,58,57,57,56,56,55,54,54,54,54,54,53,53,52,52,52,52,52,51,50,49,48,46,45,44,43,43,43,42,41

import numpy as np
x = [81,81,80,80,80,80,78,78,78,77,77,75,75,74,74,74,73,72,72,71,71,71,
             71,70,70, 70,70,68,68,68,68,68,67,67,67,66,66,65,65,65,65,64,64,64,
             64,64,64,63,63,63,63,62,62,62,62,62,61,61,61,60,60,59,59,59,59,59,
             59,59,59,58,58,57,57,56,56,55,54,54,54,54,54,53,53,52,52,52,52,52,
             51,50,49,48,46,45,44,43,43,43,42,41]
freq, bin_edges = np.histogram(x, bins=5,density=True)
print(bin_edges)
print(freq)
Show expected output
[41. 49. 57. 65. 73. 81.]
[0.01125 0.0225  0.04    0.03    0.02125]

Interpretation from the above output:

classesrel-freq
41 - 490.01125
49 - 570.0225
57 - 650.04
65 - 730.03
73 - 810.02125

key: 41 - 49 is [41,49)

0.04=4% of the Senators are aged between 75 to 65

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.