Python Forum
Saving data from each row into separate txt
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving data from each row into separate txt
#1
Hello dear forum members,

I am seeking your help with the following task. I have a data frame, in which column -fips- contains a 5 digit number and column -text- contains some text. My goal is to save the contents of each -text- row in to a separate TXT file, such that the name of each TXT is the 5-digit number from the -fips- field. 

E.g.:
fips          text
01234      My text

The expected output should be 01234.txt contacting "My text"

I tried using the following code. But it does the job incorrectly:  (a) names the TXT with index number, and (b) saves contents of the -fips- row. I realize something is wrong with "[1][1]" part but cannot figure out what.
import pandas as pd

for x in test.iterrows():
    pd.DataFrame([x[1][1]]).to_csv(str(x[1][0])+".txt", header=False, index=False)
Thank you in advance for your help.
Reply


Messages In This Thread
Saving data from each row into separate txt - by kiton - May-12-2017, 09:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving data into xlxs colomwise erdemath 0 1,458 Oct-02-2021, 09:01 AM
Last Post: erdemath
  Data saving structure JosefFilosopio 0 2,171 May-04-2019, 10:48 AM
Last Post: JosefFilosopio
  Problem with saving data and loading data to mysql kirito85 4 4,025 Feb-08-2019, 10:53 AM
Last Post: kirito85
  Saving data into .mat (Matlab Fiile) Tulo_Stone 0 2,442 Feb-28-2018, 12:57 AM
Last Post: Tulo_Stone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020