![]() |
Counting in a csv file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Counting in a csv file (/thread-29241.html) |
Counting in a csv file - standenman - Aug-24-2020 I have a csv file for which I would like to create a dictionary with "word" and "Count" within a column. So if I have Column "Name" with "Parkland Hospital", "Parkland Memorial" I want a dictionary like- dict([('Parkland',2), (Hospital', 1), ('Memorial', 1)]). Then I want to create a dict from the csv of 2 word phrases and count them, but only where the counts from the first go around of a word is less than X. In other words, I do not want to count phrases where that include words that are very frequent, on the basis that they will not be very meaningful since so frequent. Thanks RE: Counting in a csv file - Larz60+ - Aug-24-2020 That looks more like a json representation of a list, not a dictionary. try json.load on the data. RE: Counting in a csv file - standenman - Aug-24-2020 Sorry I was not being clear. All I want to do a take this csv file: Column Names "Parkland Memorial Hospital" "Parkland Institute Hospital Center" and get pairs, as in: Parkland, 2; Memorial, 1; Hospital, 2; Institute, 1; Center, 1. RE: Counting in a csv file - micseydel - Aug-24-2020 I would expect you could just pass the file object to Counter. Worst-case, you'd have to strip the newlines off first. RE: Counting in a csv file - standenman - Aug-24-2020 Yes I guess I really don't what I am doing here. My csv file has a column, "Facility Name" for which I want to count words. This code gets me the name of the column, not the data: import pandas as pd from collections import Counter myVar=pd.read_csv(r'C:\Users\Owner\Documents\UberUpdate\Hospital_General_Information.csv', sep = ",", usecols = [1]) MyCounter=Counter() MyCounter.update(myVar) print(MyCounter) Result is - Counter({'Facility Name': 1}) |