试图创建一个单词计数器,但我的语句在 python 中给出了一个类型错误
Trying to create a word counter but my statement gives a typeerror in python
python 广告的新手试图了解如何解决此问题-
import pandas as pd
import pyodbc
from collections import Counter, defaultdict
import numpy as np
conn = pyodbc.connect('Driver={SQL Server};'
'Server=10.50.3.110\ds;'
'Database=xx;'
'UID=xx;'
'PWD=xx@#Fxx*xx>m;'
)
transcripts = pd.read_sql_query("SELECT * FROM forward.claims", conn)
conn.close()
transcripts['Transcript'] = transcripts['Transcript'].str.lower()
Terms = [
['deductible','deductible'],
['deductible','deduct'],
['deductible','deducted'],
['pre-approved','pre approved'],
['pre-approved','pre approve'],
['medical records','medical records'],
['medical records','medical record'],]
Terms = pd.DataFrame(Terms, columns = ['Terms', 'Keyword'])
list_of_results = []
count = defaultdict(lambda: 0)
for index, row in transcripts.iterrows():
for string_to_search in Terms['Keyword'].to_list():
if string_to_search in row['Transcript']:
count[string_to_search] += row['Transcript'].count(string_to_search)
list_of_results.append((row['InteractionId'],string_to_search))
Traceback (most recent call last):
File "<ipython-input-20-88446cec2859>", line 5, in <module>
if string_to_search in row['Transcript']:
TypeError: argument of type 'NoneType' is not iterable
transcripts
Out[22]:
InteractionId ... Transcript
0 200323337020200120 ... donald disease
1 200323337020200120 ... does the same thing like the 250 well it's a 2...
2 200323337020200120 ... do you guys have somebody speak spanish
3 200323337020200129 ... do you want me to get a translator
[39024 rows x 5 columns]
为什么会出现这个错误,我该如何解决这个错误?是否有针对此特定类型错误的通用解决方案?另请在上面找到成绩单的代表
您至少有一行 row['Transcript']
实际上只是 None
。
您可以通过多种方式之一避免此错误,但最简单的可能是首先在相同的逻辑子句中检查这种情况。
所以不是 -
if string_to_search in row['Transcript']:
尝试-
if row['Transcript'] is not None and string_to_search in row['Transcript']:
正如其他人所说,row['Transcript']
在某些时候是 None
。事先检查此表达式是否为 not None
会有所帮助。如果您正在寻找一个优雅的解决方案,我只是想给您一个提示:collections.Counter()
python 广告的新手试图了解如何解决此问题-
import pandas as pd
import pyodbc
from collections import Counter, defaultdict
import numpy as np
conn = pyodbc.connect('Driver={SQL Server};'
'Server=10.50.3.110\ds;'
'Database=xx;'
'UID=xx;'
'PWD=xx@#Fxx*xx>m;'
)
transcripts = pd.read_sql_query("SELECT * FROM forward.claims", conn)
conn.close()
transcripts['Transcript'] = transcripts['Transcript'].str.lower()
Terms = [
['deductible','deductible'],
['deductible','deduct'],
['deductible','deducted'],
['pre-approved','pre approved'],
['pre-approved','pre approve'],
['medical records','medical records'],
['medical records','medical record'],]
Terms = pd.DataFrame(Terms, columns = ['Terms', 'Keyword'])
list_of_results = []
count = defaultdict(lambda: 0)
for index, row in transcripts.iterrows():
for string_to_search in Terms['Keyword'].to_list():
if string_to_search in row['Transcript']:
count[string_to_search] += row['Transcript'].count(string_to_search)
list_of_results.append((row['InteractionId'],string_to_search))
Traceback (most recent call last):
File "<ipython-input-20-88446cec2859>", line 5, in <module>
if string_to_search in row['Transcript']:
TypeError: argument of type 'NoneType' is not iterable
transcripts
Out[22]:
InteractionId ... Transcript
0 200323337020200120 ... donald disease
1 200323337020200120 ... does the same thing like the 250 well it's a 2...
2 200323337020200120 ... do you guys have somebody speak spanish
3 200323337020200129 ... do you want me to get a translator
[39024 rows x 5 columns]
为什么会出现这个错误,我该如何解决这个错误?是否有针对此特定类型错误的通用解决方案?另请在上面找到成绩单的代表
您至少有一行 row['Transcript']
实际上只是 None
。
您可以通过多种方式之一避免此错误,但最简单的可能是首先在相同的逻辑子句中检查这种情况。
所以不是 -
if string_to_search in row['Transcript']:
尝试-
if row['Transcript'] is not None and string_to_search in row['Transcript']:
正如其他人所说,row['Transcript']
在某些时候是 None
。事先检查此表达式是否为 not None
会有所帮助。如果您正在寻找一个优雅的解决方案,我只是想给您一个提示:collections.Counter()