0

I would like to save the sample output from this loop to CSV

[(16.0, 'thermionic vacuum diode device'), (4.0, 'adjustable electrodes')]
[(16.0, 'thermionic vacuum diode device'), (4.0, 'adjustable electrodes')]
[(9.0, 'heat transfer device')]

This output are generated from

def write_csv():
    for index, row in df.iterrows():
        text = row
        r.extract_keywords_from_sentences(text)
        r.get_ranked_phrases()
        score = r.get_ranked_phrases_with_scores()
        new_df = pd.DataFrame({'col':score})
        new_df = df.append(df)
        #print(score)
    df.to_csv("test_score_2.csv")

When I call def write_csv(), the CSV file contains only the last line, but I want to save all rows to the CSV file.

Is any suggestion?

1 Answer 1

1

You are overwriting your df at each step.

def write_csv():
    data = []
    for index, row in df.iterrows():
        text = row
        r.extract_keywords_from_sentences(text)
        r.get_ranked_phrases()
        score = r.get_ranked_phrases_with_scores()
        data.append({'col': score})

     df = pd.DataFrame(data)
     df.to_csv("test_score_2.csv")

Your append logic might have worked if there was a df variable in the first place. However, there is none.

Remember, assigning a value to a variable wipes whatever it used to point to. You're re-assigning the variable to point to a new object in memory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.