0

I am trying to use writer.writerow to present data from an array to an csv file. I have an array sum_balance and apparently I need to convert it into a numpy array before I can use the writer.writerow function. Heres my code:

numpy_arr = array(sum_balance)
with open("output.csv", "wb") as csv_file:
    writer = csv.writer(csv_file, delimiter=',')
    for element in numpy_arr:
        writer.writerow(element)
csv_file.close()

But I still get the error: writer.writerow(element)_csv.Error: iterable expected, not numpy.float64

1 Answer 1

2

The numpy iterator seems to be iterating over elements, not rows, which is why you're getting an error. However, there's an even simpler way to achieve what you're trying to do: numpy has a routine savetxt that can write an ndarray to a csv file:

output_array = np.array(my_data)
np.savetxt("my_output_file.csv", output_array, delimiter=",")
Sign up to request clarification or add additional context in comments.

2 Comments

It prints it row by row, is it possible to print it column by column?
Yes. Any numpy.ndarray can be transposed with array_name.T. So to write with columns as rows you just need to do np.savetext("output_file_name", array_name.T, delimiter=',').

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.