I am trying to write a function that reads a CSV file of students volunteers with different degrees. The aim of the function is to create a dictionary where keys are the degrees and the values are the frequency of the degrees.
Data is organized as follows;
name degree email
ABC PhD. abd@gmail.com
CDE Ph.D. cde@gmail.com
FGH MD,PHD fgh@gmail.com
Aim to get a dictionary as follows:
#degree_count{'phd':3,'md':1}
def degree_frequency(csv_file):
f = open('csv_file')
csv_f = csv.reader(f)
#Creating a list to store all the degrees from the csv file
student_degree_list=[]
#Creating an empty dictionary to count the frequency
degree_count={}
for row in csv_f:
student_degree_list.append(row[1])
#Replacing fullstops to account for variations in writing degrees ( eg JD vs J.D)
[word.replace(".", "") for word in student_degree_list]
[word.lower() for word in student_degree_list]
for ele in student_degree_list:
if ele in degree_count:
degree_count[ele]=degree_count[ele]+1
else:
degree_count[ele]=0
return degree_count