1

I have a df like this:

Allotment   Year    NDVI     A_Annex    Bachelor
A_Annex     1984    1.0      0.40       0.60
A_Annex     1984    1.5      0.56       0.89
A_Annex     1984    2.0      0.78       0.76
A_Annex     1985    3.4      0.89       0.54
A_Annex     1985    1.6      0.98       0.66
A_Annex     1986    2.5      1.10       0.44
A_Annex     1986    1.7      0.87       0.65

and I want to write each column to a new dataframe, and a subsequent csv based on the column name. So I want a new csv for the contents of each column.

so far I have done this:

outpath='C:\'
for column in df:
    x=pd.DataFrame(df[column])
    outpath=outpath + column + '.csv'
    x.to_csv(outpath)

but column contains every single column name, when I only want to write to separate ones. My desired file names would be something like Allotment.csv, Year.csv, NDVI.csv, A_Annex.csv, Bachelor.csv

2
  • I won't add an answer since you already accepted one, but you could do this in one line: for column in df: df[column].to_csv( 'C:\' + column + '.csv') Commented Feb 22, 2016 at 1:42
  • ah, good to know and much simpler, thank you. Just curious, if I wanted two send to columns at once to a new csv how would I do this? e.g. If I wanted to send Year and A_Annex together? Commented Feb 22, 2016 at 1:46

1 Answer 1

1

This line is collecting all the column names:

outpath = outpath + column + '.csv'

because it is run on every loop of the for loop

This works:

outpath='C:\'
for column in df:
     x=pd.DataFrame(df[column])
     outfile=outpath + column + '.csv'
     x.to_csv(outfile)

Notice the loop variable name is changed from outpath to a new var outfile which will prevent the filenames from being aggregated and mangled.

I noted the .csv files have two columns, the row number and the value. If you don't want the row numbers, use x.to_csv(outfile, index=False)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.