Load a .csv file into a numpy array as a column vector
<p>I've got a .csv file that I'm trying to load into a numpy array as a column vector. When using </p>

<pre><code>temp = np.genfromtxt(location, delimiter=',')
</code></pre>

<p>the data is a <code>numpy array</code> where the entries are represented as follows:</p>

<pre><code>[ 0.  ...  0.  1.  0.  ... 0.], [ 0.  ...  1.  0.  0.  ... 0.]
</code></pre>

<p>whereas, I would like something like this: </p>

<pre><code>[array([[0],
       [0],
       [0],
       ...
       [0],
       [1],
       [0],
       ...
       [0],
       [0]]), array([[0], 
       [0],
       [0],
       ...
       [0],
       [1], 
       [0],
       ...)]
</code></pre>

<p>I'm able to accomplish this using</p>

<pre><code>my_data = []

for i in range(len(temp)):
    foo = np.array([temp[i]]).T
    my_data.append(foo)
</code></pre>

<p>But am wondering if there is a more efficient way of achieving the required result.</p>

<p>Add a new axis to <code>temp</code>, and cast sublists to <em>np arrays</em>:</p>

<pre><code>temp = np.genfromtxt(location, delimiter=',')
my_data = map(np.array, temp[:, :, np.newaxis])
</code></pre>

<hr>

<p>For Python 3, call <code>list</code> on the above result or use a list comprehension:</p>

<pre><code>my_data = [np.array(arr) for arr in temp[:, :, np.newaxis]]
</code></pre>

python arrays csv numpy