import csv
from collections import Counter
columns = defaultdict(list) # each value in each column is appended to a list
with open('csv_file.csv') as f:
reader = csv.DictReader(f) # read rows into a dictionary format
for row in reader: # read a row as {column1: value1, column2: value2,...}
for (k,v) in row.items(): # go over each column name and value
columns[k].append(v) # append the value into the appropriate list
# based on column name k
选项1output_dict_counter_version = dict(Counter(degree_list_clean))
print(output_dict_counter_version)
选项2degree_frequency_dict = {}
for deg in degree_list_clean:
if deg in degree_frequency_dict:
degree_frequency_dict[deg] += 1
else:
degree_frequency_dict[deg] = 1
print(degree_frequency_dict)
使用import pandas as pd
from collections import Counter
data = pd.read_csv("csv_file.csv")
degree_list = data['degree'].tolist()
degree_list_clean = []
for cad_degrees in degree_list:
cad_degrees_lst = cad_degrees.split()
for degree in cad_degrees_lst:
degree_clean = degree.strip().replace('.','').lower()
degree_list_clean.append(degree_clean)
print(dict(Counter(degree_list_clean)))
'''
Input
name,degree,email
ABC,PhD. ,abd@gmail.com
CDE,Ph.D. ,cde@gmail.com
FGH, MD PHD ,fgh@gmail.com
Output
{'phd': 3, 'md': 1}
'''