Python Forum
how often does a word occur in this column?
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how often does a word occur in this column?
#3
Great answer @Lars60+. I thnk the newlines should be replaced by vertical bars before parsing (see 'Adventure\n' in line one).
from collections import Counter

data = 'Genres\nAction|Adventure\nAction|Adventure|Animation\nAction|Adventure|Animation|Comedy|Drama\n' \
    'Action|Adventure|Animation|Comedy|Family\nAction|Adventure|Animation|Drama|Family\n' \
    'Action|Adventure|Animation|Family\nAction|Adventure|Animation|Family|Fantasy\n' \
    'Action|Adventure|Animation|Family|Mystery\nAction|Adventure|Animation|Family|Science Fiction\n' \
    'Action|Adventure|Animation|Fantasy\nAction|Adventure|Animation|Fantasy|Horror\n' \
    'Action|Adventure|Animation|Fantasy|Science Fiction\n'
    
data_list = data.replace('\n', '|')
data_list = data_list.strip().split('|')
print(Counter(data_list).most_common(1))
Output:
[('Action', 12)]
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Messages In This Thread
RE: how often does a word occur in this column? - by ljmetzger - Jun-10-2018, 05:56 PM

Forum Jump:

User Panel Messages

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