Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pycharm计算csv中特定单词的出现次数_Python_Csv - Fatal编程技术网

Python 使用pycharm计算csv中特定单词的出现次数

Python 使用pycharm计算csv中特定单词的出现次数,python,csv,Python,Csv,我在csv文件中有三列,我想遍历“title”列并计算特定单词的出现次数,所以我开始编码,但我得到了一个错误。代码是: import csv import collections Title = collections.Counter() with open('Green Occupations.csv') as input_file: for row in csv.reader(input_file, delimiter=';'): Title[row[1]] +=

我在csv文件中有三列,我想遍历“title”列并计算特定单词的出现次数,所以我开始编码,但我得到了一个错误。代码是:

import csv
import collections

Title = collections.Counter()
with open('Green Occupations.csv') as input_file:
    for row in csv.reader(input_file, delimiter=';'):
        Title[row[1]] += 1

print 'Number of word "..": %s' % Tiltle['wind']
print Title.most_common()
我得到了这个错误:

Title[row[1]] += 1
IndexError: list index out of range
我所拥有的数据的一个例子

+------------+---------------------------------+-------------------------+
|  SOC Code  |              Title              |  Occupational Category  |
+------------+---------------------------------+-------------------------+
| 11-1011.03 | Chief Sustainability Officers   | New & Emerging          |
| 11-1021.00 | General and Operations Managers | Enhanced Skills         |
+------------+---------------------------------+-------------------------+

有什么想法吗?:)

试试下面的代码

def get_count(title):
    count=0
    title=title.lower()
    f=open('Green Occupations.csv')
    l3=[[s.strip() for s in lines.split(',')] for lines in f.readlines()]
    l4=[item[x] for item in l3]
    for item in l4:
        if item.split(' ')[0].strip('"').lower()==title:
            count+=1
    return count
print(get_count('Industrial'))
第x列和以上列表中的关联标题给出了标题列表 如果第3列中的标题将x替换为3

occurence=get_count(title=)
# will return no of occurence starting with title 
你会用熊猫吗? 这将使工作变得非常简单:

import pandas as pd

#Import data from csv
df = pd.read_csv(input_file, delimiter=';')

search_word = 'Officer'  #example

# Check if each title contains the specified word and then count
counts = df['Title'].str.contains(search_word).sum()

请举一个csv文件的示例,您可能希望在访问该行之前检查该行的长度
if len(row)<2:continue
。标题示例:原始:风能项目经理,但第一列有代码#,第三列有文本,如:New Green请在帖子中复制并粘贴一些完整的csv文件。如果包含敏感数据,请用类似文本替换。我们不喜欢猜测。我不知道我应该在哪里添加这个?我试过了,仍然得到一个错误,上面写着:TypeError:“builtin_function_或_method”对象不是iterableError,“builtin_function_或_method”是不可编辑的,因为我错过了我现在编辑的f.readlines方法的括号。很抱歉,仍然没有得到X部分。你这是什么意思?我应该把3放在哪里。我是这方面的初学者,正在尝试通过myselftitles=[item.strip().split(',')[x]来学习f.readlines()]中的项,在这里,根据您的数据,它已经被0替换。始终从0开始计数,第三列的值x=2。如果你是初学者,在你学会游泳之前不要跳入大海。尽可能多地遵循教程,站在pythons的世界里,开始从python书籍中学习,例如从o'reilly.com,你可以在那里找到好的材料。是的,我理解,但我的顾问让我为我的项目做这件事。我这样做了,结果是0我不明白为什么