For a current project, I want to iterate over a list of word pairs that are defined as common_words
within a Pandas DataFrame.
When calling the line for word in common_words:
, I am however receiving the error TypeError: 'NoneType' object is not iterable
. I have already checked for possible approaches to get this solved but have not found any solution yet.
Is there any smart tweak to make this run?
The corresponding code section looks like this:
# 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])
And get_top_n_bigram_Group2
is defined as follows:
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)