[英]how to counter the string occurrence in a csv file's column
我在该文件中有一个 csv 文件,我需要对抗字符串的出现。
例如这是一个示例文件
column_A
Abhi
Spidy
Abhi
Max
所以在上面的 csv 文件中,我需要遵循 output:
output:
Abhi 2
Spidy 1
Max 1
嗨已经试过这个
import csv
import collections
data = collections.Counter()
print(data)
with open('file.csv') as input_file:
for row in csv.reader(input_file, delimiter=';'):
data[row[1]] += 1
print('%s' % data['columnA'])
print(data.most_common())
嘿,我看到你是用计数器做的,我用 pandas 只是为了读取 csv 文件:
dict_final = {}
list_intermed = []
df = pd.read_csv('test.csv')
for i in df.A:
if i in list_intermed:
dict_final[i] += 1
else:
dict_final[i] = 1
list_intermed.append(i)
print(dict_final)
这将以字典的形式给出 output
仅使用 CSV 模块:
import csv
list1 = []
final_dict = {}
list2 = []
with open('test.csv', newline='') as csvfile:
csv_obj = csv.reader(csvfile, delimiter=',')
for row in csv_obj:
list1.append(', '.join(row).split()) #so we get a list of all rows
for i in list1[1:]: #excluding the first row as those are column names
if i[0][:-1] in list2: #i[0][:-1] takes the rows first element till the second last character the reason we take second last character is so that we don't include a comma that comes in the list items (rest of the code is same)
final_dict[i[0][:-1]] += 1
else:
final_dict[i[0][:-1]] = 1
list2.append(i[0][:-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.