1

I have a for loop in python and at the end of each step I want the output to be added as a new column in a csv file. The output I have is a 40x1 array. So if the for loop consists of 100 steps, I want to have a csv file with 100 columns and 40 rows at the end. What I have now, at the end of each time step is the following:

    with open( 'File name.csv','w') as output:
               writer=csv.writer(output, lineterminator='\n')
               for val in myvector:
                      writer.writerow([val])

However, this creates different csv files with 40 rows and 1 column each. How can have I add them all as different columns in the same csv file? This will save me a lot of computation time so any help would be very much appreciated.

smac89
  • 39,374
  • 15
  • 132
  • 179
Andriana
  • 367
  • 3
  • 8
  • 17

2 Answers2

0

Just because it is not stated clearly in your OP, I would break my answer in two cases:

1. myvector is a list of lists:

Taken from here: Rotating a two-dimensional array in Python

    with open( 'File name.csv','w') as output:
               writer=csv.writer(output, lineterminator='\n')
               for val in list(zip(*myvector[::-1])):
                      writer.writerow(val)

2. myvector is a DataFrame:

    with open( 'File name.csv','w') as output:
               writer=csv.writer(output, lineterminator='\n')
               for val in myvector.transpose():
                      writer.writerow(val)
GSazheniuk
  • 1,340
  • 10
  • 16
0

Do you need to write each column to the file individually? If not, I'd store all the data in a DataFrame and then write the DataFrame to a CSV file all at once. You should be able to copy and paste this example and run it after changing the file name.

import pandas as pd

# Sample column data (ints 0 through 39)
my_vector = range(0, 40)

# Set up empty DataFrame
data = pd.DataFrame({'Column_1' : []})

for i in range(0, 100):
    # Set the ith column equal to the column data we have
    data['Column_' + str(i)] = my_vector

data.to_csv('File name.csv', index=False, header=False)