May-12-2017, 09:23 PM
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.
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.