Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting in a csv file
#1
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
Reply
#2
That looks more like a json representation of a list, not a dictionary.

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


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020