简体   繁体   中英

Saving output from defined for loop function - redirect print output not working

I have pre-processed some text from a csv file that is labeled by different techniques used for a task, and have created a new column of the clean text per technique. I want to find the word frequency of the descriptions (ie, clean text) per technique in descending order (they have already been grouped) and did the following:

from collections import Counter

df['Count'] = df['clean_text'].str.lower().split().apply(Counter)
count = df['Count']

def most_common():
    for i in count:
    x = Counter(i)
    print(x.most_common())

When I call on most_common() I successfully get the word frequency of each technique in descending order as an output, but I want to save this output to a file or use the output to create a new column in my dataframe. I have looked into redirecting the print output using the open command etc. and saving it to python but it does not show all the outputs, it just shows the first. Can anyone help with saving the output of this function so it can be added to the dataframe or saved as its own csv?

For reference, this is one piece of code I previously tried:


for i in count:
    with open("randomfile.txt", "w") as external_file:
        x = Counter(i)
        print(x.most_common(), file=external_file)
        external_file.close()

Well try this, with the open file command, before saving it try to sort if first in an array or in a list you wanted, that should work.

make this one as your reference

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM