Foundation of Data Science: Unit IV: Python Libraries for Data Wrangling

Fancy Indexing

Python Libraries for Data Wrangling

With NumPy array fancy indexing, an array can be indexed with another NumPy array, a Python list, or a sequence of integers, whose values select elements in the indexed array.

Fancy Indexing

• With NumPy array fancy indexing, an array can be indexed with another NumPy array, a Python list, or a sequence of integers, whose values select elements in the indexed array.

•  Example: We first create a NumPy array with 11 floating-point numbers and then index the array with another NumPy array and Python list, to extract element numbers 0, 2 and 4 from the original array :

importnumpy as np

A = np.linspace(0, 1, 11)

print(A)

print(A[np.array([0, 2, 4])])

# The same thing can be accomplished by indexing with a Python list

print(A[[0, 2, 4]])

Output:

[0.         0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 ].

[0.         0.2 0.4]

[0.         0.2 0.4]

Foundation of Data Science: Unit IV: Python Libraries for Data Wrangling : Tag: : Python Libraries for Data Wrangling - Fancy Indexing