0

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())
2
  • row[0] if you want the first column, indexes start at 0 not 1 Commented Mar 1, 2021 at 6:41
  • Thank you, but I need to check with the column not with row Commented Mar 1, 2021 at 6:44

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Aryan, but what is the if i in l1:
Oh thats a typo, i reformated the variable names to make it more understandable, must have left that one out, let me just make a quick edit
do you have the answer with csv module
Sure, edited my answer (I literally just looked up the documentation for csv module, so this might not be the best code)
you made my day bro, I don't know this code will be working or not but thanks for your effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.