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

14,790

Solution 1

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.

Solution 2

It's still inefficient, but since it's required here's one possible solution.

import pandas as pd
from io import StringIO

data="""
column1 column2
c1 c2
c3 c4
c5 c6
"""

df = pd.read_csv(StringIO(data), delimiter='\s+')

i=0
for row in df.values:
    filename = 'testdir/review{}.csv'.format(i)
    row.tofile(filename, sep=",", format="%s")
    i+=1

This will take the values as an array and write the data to a csv file named review0.csv, review1.csv... Another solution is to use pd.to_csv within the loop and specify the chunk

Share:
14,790
Wolf
Author by

Wolf

Updated on June 14, 2022

Comments

  • Wolf
    Wolf almost 2 years

    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.

    • Leb
      Leb over 8 years
      That's going to be very inefficient, what's the purpose for that?
    • Wolf
      Wolf over 8 years
      Just to perform classification. My requirement is in that way
    • R Nar
      R Nar over 8 years
      make a file name variable that changes every time you write a new line then open that filename with the w parameter
    • Wolf
      Wolf over 8 years
      Can you please suggest the format to write data from dataframe to text file ? @RNar I've been wondering on that for quite a while. Does to_csv work for this ?
    • R Nar
      R Nar over 8 years
      i wouldnt suggest it, no. because you want to write a new file for each row, iterate through the rows then just have something like f = open(filename, 'w') then f.write(row) kind of thing. just make sure to change filename each time.
  • Laurie
    Laurie almost 6 years
    For anyone else receiving the Unicode Error: change f = open(str(i)+'.txt', 'w'), to f = open(str(i)+'.txt', 'w', encoding='utf-8')