Reputation: 25
I have a csv file in that file I need to counter the occurrence of the string.
for example this is a sample file
column_A
Abhi
Spidy
Abhi
Max
so in the above csv file I need to have to following the output:
output:
Abhi 2
Spidy 1
Max 1
Hi have tried with this
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())
Upvotes: 0
Views: 47
Reputation: 117
Hey I see you did it using counter, Ive used pandas just to read the csv file:
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)
Which will give the output in the form of a dictionary
Using only CSV module :
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])
Upvotes: 1