1

I try to write my data to a csv file multiple times, and each time on a new column.

The code I used as follow:

for i in range(2):
    loss = np.array([np.random.rand(10) + i, ])
    df = pd.DataFrame(loss.T)
    df.to_csv('res.csv', index=False, mode='a')

However, it did not write on a new column the second time, but under the columns of the first time.

What can I do to make it write to a new column?

Whitney
  • 11
  • 2
  • perhaps this helps, https://stackoverflow.com/questions/17530542/how-to-add-pandas-data-to-an-existing-csv-file – Yuca Dec 11 '20 at 18:22
  • 1
    Good question, but I don't think it's possible. Probably you'll gonna need to read the data, concatenate the columns, then write it back to the file. Or you can use the python built-in functions like in [this question](https://stackoverflow.com/questions/39888949/fastest-way-to-concatenate-multiple-files-column-wise-python). – Cainã Max Couto-Silva Dec 11 '20 at 22:13
  • why you need to do it that way? – Pablo C Dec 12 '20 at 03:16

0 Answers0