Count unique values in csv column without pandas

Question:

I’m using python3 and I want to count unique values in column without pandas

For example if I have this csv:

Name,Surname,PCName,File_Name
Max,Petrov,wrs123,test2.csv
Ivan,Ivanov,wrs321,test2.csv
Vasily,Sidorov,wrs223,test3.csv
Alex,Dmitriev,wrs331,test3.csv
Alexey,Dmitriev,wrs333,test3.csv

With pandas I’ve got this code:

import pandas as pd
df = pd.read_csv('C:/tmp/test.csv', index_col=0)
print(df["Surname"].value_counts())

And this result:

Dmitriev    2
Petrov      1
Ivanov      1
Sidorov     1
Asked By: Maxim Oleynik

||

Answers:

For example, you can use csv module to read and process your data:

import csv
from collections import defaultdict

counter = defaultdict(int)
with open('test.csv') as file:
    reader = csv.DictReader(file)
    for row in reader:
        counter[row['Surname']] += 1
        
print(dict(counter))

Output:

{'Petrov': 1, 'Ivanov': 1, 'Sidorov': 1, 'Dmitriev': 2}
Answered By: funnydman

You can make use of pandas for this. It makes life simpler and easier

Code :

import pandas as pd

df = pd.read_csv("/content/sample_data/abc.csv")
get_count = df.pivot_table(columns=['Surname'], aggfunc='size')
print(get_count)

Output :

Output

Answered By: codeholic24
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.