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
CodePudding user response:
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}