Slicing Cross-section of 2D NumPy array
<p>I'm looking to print a cross-section of a numpy array. I am looking to pull the first 15 rows and only data from the 5th index column.</p>

<pre><code>import csv as csv
import numpy as np 

csv_file_object = open('train.csv', 'rU')
header = next(csv_file_object)
data = []

for row in csv_file_object:
    data.append(row)

data = np.array(data)

print(data[0:15,5])
</code></pre>

<p>When I run the above code, I receive the following error: IndexError: too many indices for array.</p>

<p>Running the code without adding the column filter works as expected. The csv file is a 12x892 (x,y) dataset.</p>

<p>The <code>csv_file_object</code> is a file object contains all lines of your csv file without splitting. You have to use <code>csv</code> module to read them properly:</p>

<pre><code>with open('train.csv', 'rU') as csv_file_object:
    reader = csv.DictReader(csv_file_object)
    data = np.array([row.values() for row in reader])
</code></pre>

<p>But since you want to convert them to a numpy array you better to use <code>genfromtxt</code> function:</p>

<pre><code>data = np.genfromtxt('train.csv')
</code></pre>

<p>You can pass arguments like <code>dtype</code>, <code>delimiter</code> and <code>names</code> (a boolean for keeping the headers).</p>

python arrays numpy indices