26

As simple as it seems, I could not find any solution for my question online. Basically, I have two arrays a and b that I want to save to a csv file. It will be two columns. I want to add the column names as well. Below code I use to dump arrays to a csv.

from np import array, savetxt

a = array([1,2,3,4])
b = array([5,6,7,8])
savetxt('submission2.csv', zip(a,b), delimiter=',', fmt='%f')

How would I add column names? I would like the csv file to look like

Name1 Name2
 1     5
 2     6
 3     7
 4     8

It is so strange that this option is not in the savetxt function. header option does do it because it just pastes a comment into the first cell. Thanks.

Edit: Arrays

2
  • The first row is where you would put column names; do you want row names instead? Can you give a sample of how you would want the file to look like? Commented Nov 3, 2015 at 5:58
  • @BurhanKhalid added details Commented Nov 3, 2015 at 6:02

5 Answers 5

34

You can do it with pandas package easily:

import pandas as pd
import numpy as np

a = np.array([1,2,3,4])
b = np.array([5,6,7,8])

df = pd.DataFrame({"name1" : a, "name2" : b})
df.to_csv("submission2.csv", index=False)
Sign up to request clarification or add additional context in comments.

1 Comment

This works, but I would think the option below (from @Burhan) is going to be quicker and more memory efficient, since you don't have to create a copy of your entire matrix as a Pandas DF
20

Use the header option, like this:

>>> import numpy
>>> a = numpy.array([[1,2],[3,4],[5,6]])
>>> numpy.savetxt("foo.csv", a, delimiter=',', header="A,B", comments="")

The resulting file looks like this:

A,B
1.000000000000000000e+00,2.000000000000000000e+00
3.000000000000000000e+00,4.000000000000000000e+00
5.000000000000000000e+00,6.000000000000000000e+00

3 Comments

is there a way to add the column to an existing csv file? Also, how can I get rid of the e?
@duldi you can change the format with the fmt kwarg. The default is %.18e which includes the e. Try %f or %g depending on your needs
fmt='%i' for an output in integers
3

Note that savetxt (and loadtxt) also takes file handles.

Hence if you want a more advanced header you can do this:

a = array([1,2,3,4])
b = array([5,6,7,8])
with open('submission2.csv','w') as f:
    f.write('# This is a very complex header\n')
    f.write('A,B\n')
    savetxt(f, zip(a,b), delimiter=',', fmt='%f')

Or, as has already been noted, use the header=str(...) argument.

3 Comments

I think you meant np.array(list(zip(a,b)))
Is the conversion to a list and then to array really necessary?
well, one can create arrays in many ways, generally this is the way people do it. :)
1

you can do like this:

import np import array, savetxt

a = array([1,2,3,4])
b = array([5,6,7,8])
f = open("submission2.csv", "w")
f.write("{},{}\n".format("Name1", "Name2"))
for x in zip(a, b):
    f.write("{},{}\n".format(x[0], x[1]))
f.close()

Comments

0

I have found a solution for saving multiple numpy 1D arrays as columns:

 import numpy as np
 data = []
 for i in single_np_arrays:
     data.append(i)
 data = np.array(data).T  #transpose the array to have proper columns
 np.savetxt('columns_from_np_arrays.csv',data,delimiter=',')

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.