利用Python进行数据分析

基础索引与切片

2019-01-14  本文已影响8人  庵下桃花仙
In [11]: arr = np.arange(10)

In [12]: arr
Out[12]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [13]: arr[5]
Out[13]: 5

In [14]: arr[5: 8]
Out[14]: array([5, 6, 7])

In [15]: arr[5: 8]
Out[15]: array([5, 6, 7])

In [16]: arr[5: 8] = 12

In [17]: arr
Out[17]: array([ 0,  1,  2,  3,  4, 12, 12, 12,  8,  9])

与 Python 内建列表不同。数组的切片是原数组的视图,意味着数据并不是被复制了,任何对于视图的修改都会反映到原数组上。

In [22]: arr_slice = arr[5: 8]

In [23]: arr_slice
Out[23]: array([12, 12, 12])

In [24]: arr_slice[1] = 12345

In [25]: arr
Out[25]:
array([    0,     1,     2,     3,     4,    12, 12345,    12,     8,
           9])

In [26]: arr_slice[:] = 64

In [27]: arr
Out[27]: array([ 0,  1,  2,  3,  4, 64, 64, 64,  8,  9])
>>> a = [1, 2, 3, 4, 5]
>>> a[0] = 6
>>> a
[6, 2, 3, 4, 5]
>>> arr = a[0]
>>> arr
6
>>> arr = 12345
>>> arr
12345
>>> a
[6, 2, 3, 4, 5]

因为 numpy 适合处理很大的数组,如果持续复制数据,会引起内存问题。实在想要一份拷贝的话,要显式地复制数组 arr[5: 8].copy()

在二维数组中,每个索引值对应的元素是一个一维数组。

In [28]: arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [29]: arr2d
Out[29]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [30]: arr2d[2]
Out[30]: array([7, 8, 9])

In [31]: arr2d[0][2]
Out[31]: 3

In [32]: arr2d[0, 2]
Out[32]: 3

在多维数组中,可以省略后续索引值,返回降低一个维度的数组。

In [2]: import numpy as np

In [3]: arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

In [4]: arr3d
Out[4]:
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

In [5]: arr3d[0]
Out[5]:
array([[1, 2, 3],
       [4, 5, 6]])

In [6]: old_values = arr3d[0].copy()

In [7]: arr3d[0] = 42

In [8]: arr3d
Out[8]:
array([[[42, 42, 42],
        [42, 42, 42]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

In [9]: arr3d[0] = old_values

In [10]: arr3d
Out[10]:
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

In [11]: arr3d[1, 0]
Out[11]: array([7, 8, 9])

数组的切片索引

In [12]: arr = np.arange(10)

In [13]: arr[5: 8] = 64

In [14]: arr
Out[14]: array([ 0,  1,  2,  3,  4, 64, 64, 64,  8,  9])

In [15]: arr[1: 6]
Out[15]: array([ 1,  2,  3,  4, 64])

In [16]: arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [17]: arr2d
Out[17]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [18]: arr2d[:2]
Out[18]:
array([[1, 2, 3],
       [4, 5, 6]])

In [19]: arr2d[:2, 1:]
Out[19]:
array([[2, 3],
       [5, 6]])

In [20]: arr2d[1, :2]
Out[20]: array([4, 5])

In [21]: arr2d[:2, 2]
Out[21]: array([3, 6])

In [22]: arr2d[:, :1]
Out[22]:
array([[1],
       [4],
       [7]])

In [23]: arr2d[:2, 1:] = 0

In [24]: arr2d
Out[24]:
array([[1, 0, 0],
       [4, 0, 0],
       [7, 8, 9]])
上一篇下一篇

猜你喜欢

热点阅读