Numpy&Pandas 003: Index

We make two new arrays firstly,

1
2
3
4
5
6
A = np.arange(0,6)
# array([0, 1, 2, 3, 4, 5])

B = A.reshape((2,3))
#array([[0, 1, 2],
# [3, 4, 5]])

One-dimensional

It mostly likes list in original Python

1
2
A[0]	#0
A[1:] #array([1, 2, 3, 4, 5])

Two-dimensional

It is also likely to Python,

1
2
3
4
5
B[0][0]	#0
B[:,1] #array([1, 4])
B[1,2] #5
B[0,:] #array([0, 1, 2])
B[0,1:3] #array([1, 2])

For array traversal, there is also a normal way

1
2
3
4
5
for row in B:
print(row)

#[0 1 2]
#[3 4 5]

If we wanna traversal the column of the array, there is not given a function to do that instance, but we can transpose the array firstly,

1
2
3
4
5
6
for column in B.T:
print(column)

#[0 3]
#[1 4]
#[2 5]

If we wanna change or output every item in the array, we would like to use an inner function called flat, but be careful, it will be a iterator.

1
2
3
4
5
6
7
8
9
10
for item in B.flat:
print(item)


#0
#1
#2
#3
#4
#5

For this function, actually, we can use it to output everything in the array, the key word needed to change flatten and the type of output is an array.

1
2
B.flatten()
#array([0, 1, 2, 3, 4, 5])

Thanks for your reading, I hope it is useful for you.


End

Illustrator / Cagy

Text / Cagy

Editor / Cagy

Design / Cagy