0

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)
Malte Susen
  • 767
  • 3
  • 13
  • 36
  • 2
    Based on the error, it looks like `common_words` is `None`. Have you checked the output of your function to verify the return? – G. Anderson Jul 14 '20 at 14:51
  • Thanks for your input and I see your point. The print output in the terminal is working though. Is there any way to assign common_words to the correct data? (you most likely need more code for that) – Malte Susen Jul 14 '20 at 14:56
  • I have appended the code for `get_top_n_bigram_Group2`. It would be great if a solution can be found why `common_words` is `None` in this case. Many thanks already in advance. – Malte Susen Jul 14 '20 at 14:59
  • 1
    You don't `return` from your function. Without a return statement, a function implicitly returns `None` – G. Anderson Jul 14 '20 at 15:04
  • 1
    Does this answer your question? [return, return None, and no return at all?](https://stackoverflow.com/questions/15300550/return-return-none-and-no-return-at-all) – G. Anderson Jul 14 '20 at 15:04

1 Answers1

1

This function doesn't return anything,

# Loop over the common words inside the JSON object
common_words = get_top_n_bigram_Group2(df[i], 500)

If you're familliar with other Languages like C, C#, C++, or Java it's like using the void keyword before a function so get_top_n_bigram_Group2() returns, void or null in other languages but python uses None and the Error

TypeError: 'NoneType' object is not iterable

Is telling you that it's not an iterator like Strings, Lists, dictionaries, or tuples, so you can't use indexing for them or in this case for loops.

12ksins
  • 307
  • 1
  • 12