Python Forum

Full Version: Counting in a csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
That looks more like a json representation of a list, not a dictionary.

try json.load on the data.
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.
I would expect you could just pass the file object to Counter. Worst-case, you'd have to strip the newlines off first.
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})