randomal
randomal

Reputation: 6592

Python - save binned data to text file

I want to save some histogram data in a csv file. This is the code I came up with:

ExportName_csv = 'ExportData/' + FileName + '.csv'
freq, bins = np.histogram(ValList,bins)
np.savetxt(ExportName_csv, izip(freq, bins), delimiter="\t")

For each bin I want to save the bin value and the corresponding count freq in ExportName_csv. I want values regarding different to be bins in different lines; in each line values are separated by a comma.

With the current code I get the error IndexError: tuple index out of range. What do you suggest to do?

Full traceback is:

np.savetxt(ExportName_csv, izip(freq, bins), delimiter="\t") 
   File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/nump‌​y/lib/npyio.py", line 976, in savetxt ncol = X.shape[1] 
IndexError: tuple index out of range

Upvotes: 3

Views: 5737

Answers (1)

alko
alko

Reputation: 48317

First, comma is ',', not \t.

Second, you should use zip, not izip:

>>> data = zip(*np.histogram(ValList,bins))
>>> np.savetxt('test.txt', data, delimeter=',')

Upvotes: 3

Related Questions