Для текущего проекта я хочу перебрать список пар слов, которые определены как common_words
в Pandas DataFrame.
При вызове строки for word in common_words:
я, однако, получаю ошибка TypeError: 'NoneType' object is not iterable
. Я уже проверил возможные подходы к решению этой проблемы, но пока не нашел никакого решения.
Есть ли какие-нибудь хитрые настройки для этого запуска?
Соответствующий раздел кода выглядит следующим образом:
# Open the file to write to
with open('sp500-1.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write headers
writer.writerow(["Section", "TFI"])
# Loop over the JSON objects
for i in ['txt_pro','txt_con','txt_adviceMgmt','txt_main']:
# Loop over the common words inside the JSON object
common_words = get_top_n_bigram_Group2(df[i], 500)
for word in common_words:
# Print and write row.
print(df2)
writer.writerow([df2])
И get_top_n_bigram_Group2
определяется следующим образом:
def get_top_n_bigram_Group2(corpus, n=None):
# settings that you use for count vectorizer will go here
tfidf_vectorizer=TfidfVectorizer(ngram_range=(2, 2), stop_words='english', use_idf=True).fit(corpus)
# just send in all your docs here
tfidf_vectorizer_vectors=tfidf_vectorizer.fit_transform(corpus)
# get the first vector out (for the first document)
first_vector_tfidfvectorizer=tfidf_vectorizer_vectors[0]
# place tf-idf values in a pandas data frame
df1 = pd.DataFrame(first_vector_tfidfvectorizer.T.todense(), index=tfidf_vectorizer.get_feature_names(), columns=["tfidf"])
df2 = df1.sort_values(by=["tfidf"],ascending=False)
print(df2)