15

I want to write data that I have to create a histogram into a csv file. I have my 'bins' list and I have my 'frequencies' list. Can someone give me some help to write them into a csv in their respective columns?

ie bins in the first column and frequency in the second column

tshepang
  • 12,111
  • 21
  • 91
  • 136
GeoMonkey
  • 1,615
  • 7
  • 28
  • 56
  • 1
    What have you tries so far? –  Oct 10 '13 at 17:47
  • What kind of data are in the bins and frequencies lists (numbers, strings, lists, dictionaries, instances of classes, etc)? You'll get better answers the more details you put in your questions. – martineau Oct 10 '13 at 18:13
  • Even better, don't just describe the data, give us some sample data, and the exact output format you want. – abarnert Oct 10 '13 at 19:02

3 Answers3

27

The original Python 2 answer

This example uses izip (instead of zip) to avoid creating a new list and having to keep it in the memory. It also makes use of Python's built in csv module, which ensures proper escaping. As an added bonus it also avoids using any loops, so the code is short and concise.

import csv
from itertools import izip

with open('some.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(izip(bins, frequencies))

The code adapted for Python 3

In Python 3, you don't need izip anymore—the builtin zip now does what izip used to do. You also don't need to open the file in binary mode:

import csv

with open('some.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(zip(bins, frequencies))
Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
  • Thanks, this worked great. I will try and add more detail into the question in future, in hindsight I can see how vague it is. – GeoMonkey Jan 23 '14 at 17:20
  • 1
    Also, in Python 3, you don't need to open it as binary: use 'w' rather than 'wb' – waternova Oct 10 '18 at 20:37
  • In python 3 you don't need to import zip you can just use it. (https://stackoverflow.com/questions/32659552/izip-not-working-in-python-3-x) – Suleka_28 Nov 02 '18 at 08:15
  • @Suleka_28 That is correct. This was mentioned in my answer, but I see there is still a lot of confusion about this so I added an explicit Python 3 example. – Ludwik Trammer Nov 03 '18 at 10:40
  • Use open(...,newline='') to prevent blank rows https://stackoverflow.com/a/21804265/1114105 – user1114 Apr 21 '21 at 18:48
4

you should use zip() http://docs.python.org/2/library/functions.html#zip

something like :

f=open(my_filename,'w')
for i,j in zip(bins,frequencies):
    f.write(str(i)+","+str(j))
f.close()
Pixou
  • 1,719
  • 13
  • 23
  • 1
    Since he hasn't told us anything about what the bin labels look like, I'd definitely want to use `csv` here. Otherwise, a bin like `"a=0-10,b=0-10"` will end up as two columns. – abarnert Oct 10 '13 at 17:54
3

Hm, am I missing something? This sounds pretty straightforward:

bins = [ 1,2,3,4,5 ]
freq = [ 9,8,7,6,5 ]

f = open("test.csv", "w")

for i in xrange(len(bins)):
    f.write("{} {}\n".format(bins[i], freq[i]))

f.close()
DirkR
  • 488
  • 2
  • 9