Writing a list of NumPy arrays to csv

save 3d numpy array to csv
append numpy array to csv
numpy save multiple arrays
save numpy array to csv
numpy save 2d array to text file
writing numpy array into csv
numpy savetxt integer
numpy savetxt columns

I have a getX() which gives a 2D array(shape=(18,2)) as an output for each frame of an image. I want to add this continuously updating array to the csv. I have used the following code. But it is giving me only one row(probably for only one frame).

[array([178.,  76.]), array([184.,  92.]), array([164.,  90.]), array([154., 116.]), array([160., 126.]), array([204.,  94.]), array([208., 124.]), array([190., 132.]), array([164., 152.])]

I have tried this:

with open("data.csv",mode ='w') as csvfile:
        wr = csv.writer(csvfile, quoting = csv.QUOTE_ALL,dialect="excel")
        get_val = oneObject.getX(currentFrameIndex)
        for  humanPos in get_val:
            wr.writerow(humanPos)

You can un-nest data with itertools.chain.from_iterable and use wr.writerow.

>>> from itertools import chain
>>> 
>>> with open("data.csv", mode='w') as csvfile:
...:      wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, dialect="excel")
...:      wr.writerow(chain.from_iterable(data))                                                                                                                 

Output:

$ cat data.csv
"178.0","76.0","184.0","92.0","164.0","90.0","154.0","116.0","160.0","126.0","204.0","94.0","208.0","124.0","190.0","132.0","164.0","152.0"

A Python list of Numpy Arrays to CSV?, Use the csv module with its writerows method: import csv with open('my_data.txt', 'w') as f: csvwriter = csv.writer(f)  NumPy Array Object Exercises, Practice and Solution: Write a NumPy program to convert a NumPy array into a csv file.

You can save the numpy array's directly to csv, you don't need any other module.

import numpy as np
array = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
numpy.savetxt("output.csv", array, delimiter=",")

To save it in single row and quote the output, you can use the ravel like @jpp suggested.

import numpy as np
array = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
np.savetxt('output.csv', np.array(array).ravel()[None], fmt='"%s"', delimiter=',')

How to save Numpy Array to a CSV File using numpy.savetxt() in , Also, instead of saving complete 2D numpy array to a csv file, if we want Save a Structured Numpy array in csv file by using list of formatting  1. 2. # Save Numpy array to csv. np.savetxt('array.csv', [arr], delimiter=',', fmt='%d') It will create a csv file with name ‘array.csv’ and store contents of numpy array in it i.e. Vim. 6,1,4,2,18,9,3,4,2,8,11. 1. 6,1,4,2,18,9,3,4,2,8,11.

You can use np.ndarray.ravel after converting your list of arrays (data) to a single array. Then use either the csv module or NumPy, depending on the precise output you require.

csv
with open('data.csv', mode='w') as csvfile:
    wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, dialect='excel')
    wr.writerow(np.array(data).ravel())

Output:

"178.0","76.0","184.0","92.0","164.0","90.0","154.0","116.0","160.0","126.0","204.0","94.0","208.0","124.0","190.0","132.0","164.0","152.0"

numpy

To save on one line you need to slice via [None]. In addition, you can specify formatting via the fmt parameter of np.savetxt. If you require text-based storage, this solution should be preferred to save and read arrays via NumPy.

np.savetxt('data.csv', np.array(data).ravel()[None], fmt='%d', delimiter=',')

Output:

178,76,184,92,164,90,154,116,160,126,204,94,208,124,190,132,164,152

How do I read CSV data into a record array in NumPy?, How do I read a csv file in a NumPy array? Saving a NumPy array as a csv file. We use the savetxt method to save to a csv. np.savetxt("saved_numpy_data.csv", my_array, delimiter=",") Reading a csv file into a Pandas dataframe. The read_csv will read a CSV into Pandas. This import assumes that there is a header row.

What makes Numpy Arrays Fast: Memory and Strides, How do I write a NumPy array to a text file? You can save your NumPy arrays to CSV files using the savetxt () function. This function takes a filename and array as arguments and saves the array into CSV format. You must also specify the delimiter; this is the character used to separate each variable in the file, most commonly a comma.

NumPy: Convert a NumPy array into a csv file, NumPy Array Object Exercises, Practice and Solution: Write a NumPy program to convert a NumPy array into a csv file. The values of the first list need to be unique and hashable. Example: def tips_to_dictionary(keys  NumPy Array Object Exercises, Practice and Solution: Write a NumPy program to convert a list and tuple into arrays.

Writing a CSV File, Python. import numpy as np # Generate random 3x4 matrix of floats y, 3x1 vector of ints d y = np.random.rand(3  In the previous chapters, we learned about reading CSV files. Writing CSV files is just as straightforward, but uses different functions and methods. Let's first generate some data to be stored in the CSV format. Generate a 3 x 4 NumPy array after seeding the random generator in the following code snippet. Set one of the array values to NaN:

Comments
  • are those list of numpy array?
  • yes. it is a nD array
  • In case it isn't clear from the answers, np.savetxt is designed to write a 2d array. Internally it is quite simple. It makes input an array if it isn't already, and then just iterates over the rows, doing a python formatted write of the tuple(row). So if your list makes a nice 2d numeric array it should work fine.
  • @timgeb that can be done, I will update the code shortly