Search code examples
pythonpandasrows

Write each row of pandas dataframe into a new text file - pythonic way


I was trying to google up if there's a way to parse a pandas dataframe row wise and write the contents of each row into a new text file. My dataframe consists of a single column called Reviews.

Review classification

I'm looking to do some sentiment analysis on movie reviews and that I need each review to be in a separate text file. Can somebody help me here.


Solution

  • I've written something like this and it works. anyways thanks for your inputs guys

    for index, row in p.iterrows():
        if i > len(p):
           break
        else:
           f = open(str(i)+'.txt', 'w')
           f.write(row[0])
           f.close()
           i+=1
    

    where p is a dataframe.