The following data represent the lifetimes (in hours) of a sample of 40 transistors:
112, 121, 126, 108, 141, 104, 136, 134
121, 118, 143, 116, 108, 122, 127, 140
113, 117, 126, 130, 134, 120, 131, 133
118, 125, 151, 147, 137, 140, 132, 119
110, 124, 132, 152, 135, 130, 136, 128
Give a cumulative relative frequency plot of these data
My approach
First I create an array of the dataset
Then I find out the frequency of each number in the array
Then I create two lists to save the values of the data and their frequencies
My code:
LifeTime = [112, 121, 126, 108, 141, 104, 136, 134,
121, 118, 143, 116, 108, 122, 127, 140,
113, 117, 126, 130, 134, 120, 131, 133,
118, 125, 151, 147, 137, 140, 132, 119,
110, 124, 132, 152, 135, 130, 136, 128]
# initializing dict to store frequency of each element
dict_fre = {}
# iterating over the elements for frequency
for element in LifeTime:
if element in dict_fre:
dict_fre[element] += 1
else:
dict_fre[element] = 1
#code to store the keys and their corresponding values to the list
keys_list=[]
val_list=[]
for i,j in dict_fre.items():
keys_list.append(i)
val_list.append(j)
Next I create a dataframe using Pandas and save the keys_list and val_list as a dictionary so that I can calculate the relative frequency and then relative cumulative frequency. Then I will plot those data.
Dataframe
import pandas as pd
d = {'life':[keys_list],'frequency':[val_list]}
df = pd.DataFrame(d)
I got this output
life frequency
0 [112, 121, 126, 108, 141, 104, 136, 134, 118, ... [1, 2, 2, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, ...
But I want to get something like that
life frequency
112 1
121 2
126 2
108 2
How could I modify my code to get the desire output?