Indexing and slicing
Slicing data is trivial with numpy. We will slice the matrice "e". Note that, in Python, you need to use the brackets to return the rows or columns
## Slice import numpy as np e = np.array([(1,2,3), (4,5,6)]) print(e) [[1 2 3] [4 5 6]]
Remember with numpy the first array/column starts at 0.
## First column print('First row:', e) ## Second colprint('Second row:', e)
First row: [1 2 3] Second row: [4 5 6]
In Python, like many other languages,
- The values before the comma stand for the rows
- The value on the rights stands for the columns.
- If you want to select a column, you need to add : before the column index.
- : means you want all the rows from the selected column.
print('Second column:', e[:,1])
Second column: [2 5]
To return the first two values of the second row. You use : to select all columns up to the second
## Second Row, two values print(e[1, :2]) [4 5]