hanugm
hanugm

Reputation: 1417

Writing series to a text file

I have a long matrix (say A) with 10,000 rows and 2 columns in an excel file.

I need to copy each column in separate text files (say A0.txt, A1.txt);

I did the following

A0 = open("A0.txt", "w+")
A1 = open("A1.txt", "w+")
A0.write(A.iloc[:,0])
A1.write(A.iloc[:,1])

but the error is

TypeError: write() argument must be str, not Series

then, how can I write the series?

Upvotes: 0

Views: 1394

Answers (1)

Mateo Torres
Mateo Torres

Reputation: 1625

You could do something like:

for col in A.columns:
    A[col].to_csv('A_'+col+'.txt', index=False, header=False)

and you will end up with one txt file per column.

Upvotes: 3

Related Questions