《Python数据分析技术栈》第05章 04 切片或选择数据子集(Slicing or selecting a subset of data)
Slicing of arrays is similar to the slicing of strings and lists in Python. A slice is a subset of a data structure (in this case, an array), which can represent a set of values or a single value.
数组的切分类似于 Python 中字符串和列表的切分。切片是数据结构(在本例中是数组)的一个子集,可以代表一组值或单个值。
Consider the following array:
请看下面的数组:
x=np.arange(0,10).reshape(5,2)
Some examples of slicing are given in the following.
下面给出了一些切片实例。
Select the first subarray [0,1]:
选择第一个子数组 [0,1]:
x[0]
Select the second column:
选择第二栏:
x[:,1]
Select the element at the fourth row and first column:
选择第四行第一列的元素:
x[3,0]
We can also create a slice based on a condition:
我们还可以根据条件创建切片:
x[x<5]
When we slice an array, the original array is not modified (a copy of the array is created).
当我们对数组进行切分时,原始数组不会被修改(会创建一个数组副本)。
Now that we have learned about creating and working with arrays, we move on to another important application of NumPy – calculation of statistical measures using various functions.
在学习了数组的创建和使用之后,我们将进入 NumPy 的另一个重要应用–使用各种函数计算统计量。