一维数据结构:Series¶

In [1]:

  1. import numpy as np
  2. import pandas as pd

Series 是一维带标记的数组结构,可以存储任意类型的数据(整数,浮点数,字符串,Python 对象等等)。

作为一维结构,它的索引叫做 index,基本调用方法为

  1. s = pd.Series(data, index=index)

其中,data 可以是以下结构:

  • 字典
  • ndarray
  • 标量,例如 5
    index 是一维坐标轴的索引列表。

从 ndarray 构建¶

如果 data 是个 ndarray,那么 index 的长度必须跟 data 一致:

In [2]:

  1. s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
  2.  
  3. s

Out[2]:

  1. a -0.032806
  2. b 0.050207
  3. c -1.909697
  4. d -1.127865
  5. e -0.073793
  6. dtype: float64

查看 index

In [3]:

  1. s.index

Out[3]:

  1. Index([u'a', u'b', u'c', u'd', u'e'], dtype='object')

如果 index 为空,那么 index 会使用 [0, …, len(data) - 1]

In [4]:

  1. pd.Series(np.random.randn(5))

Out[4]:

  1. 0 -0.376233
  2. 1 -0.474349
  3. 2 1.660590
  4. 3 0.461434
  5. 4 0.190965
  6. dtype: float64

从字典中构造¶

如果 data 是个 dict,如果不给定 index,那么 index 将使用 dictkey 排序之后的结果:

In [5]:

  1. d = {'a' : 0., 'b' : 1., 'c' : 2.}
  2.  
  3. pd.Series(d)

Out[5]:

  1. a 0
  2. b 1
  3. c 2
  4. dtype: float64

如果给定了 index,那么将会按照 index 给定的值作为 key 从字典中读取相应的 value,如果 key 不存在,对应的值为 NaN(not a number, Pandas 中的缺失默认值):

In [6]:

  1. pd.Series(d, index=['b', 'd', 'a'])

Out[6]:

  1. b 1
  2. d NaN
  3. a 0
  4. dtype: float64

从标量值构造¶

如果 data 是标量,那么 index 值必须被指定,得到一个值为 dataindex 等长的 Series

In [7]:

  1. pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])

Out[7]:

  1. a 5
  2. b 5
  3. c 5
  4. d 5
  5. e 5
  6. dtype: float64

像 ndarray 一样使用 Series¶

In [8]:

  1. s

Out[8]:

  1. a -0.032806
  2. b 0.050207
  3. c -1.909697
  4. d -1.127865
  5. e -0.073793
  6. dtype: float64

支持数字索引操作:

In [9]:

  1. s[0]

Out[9]:

  1. -0.032806330572971713

切片:

In [10]:

  1. s[:3]

Out[10]:

  1. a -0.032806
  2. b 0.050207
  3. c -1.909697
  4. dtype: float64

mask 索引:

In [11]:

  1. s[s > s.median()]

Out[11]:

  1. a -0.032806
  2. b 0.050207
  3. dtype: float64

花式索引:

In [12]:

  1. s[[4, 3, 1]]

Out[12]:

  1. e -0.073793
  2. d -1.127865
  3. b 0.050207
  4. dtype: float64

支持 numpy 函数:

In [13]:

  1. np.exp(s)

Out[13]:

  1. a 0.967726
  2. b 1.051488
  3. c 0.148125
  4. d 0.323724
  5. e 0.928864
  6. dtype: float64

像字典一样使用 Series¶

也可以像字典一样使用 Series

In [14]:

  1. s["a"]

Out[14]:

  1. -0.032806330572971713

修改数值:

In [15]:

  1. s["e"] = 12.
  2.  
  3. s

Out[15]:

  1. a -0.032806
  2. b 0.050207
  3. c -1.909697
  4. d -1.127865
  5. e 12.000000
  6. dtype: float64

查询 key

In [16]:

  1. "e" in s

Out[16]:

  1. True

In [17]:

  1. "f" in s

Out[17]:

  1. False

使用 key 索引时,如果不确定 key 在不在里面,可以用 get 方法,如果不存在返回 None 或者指定的默认值:

In [18]:

  1. s.get("f", np.nan)

Out[18]:

  1. nan

向量化操作¶

简单的向量操作与 ndarray 的表现一致:

In [19]:

  1. s + s

Out[19]:

  1. a -0.065613
  2. b 0.100413
  3. c -3.819395
  4. d -2.255729
  5. e 24.000000
  6. dtype: float64

In [20]:

  1. s * 2

Out[20]:

  1. a -0.065613
  2. b 0.100413
  3. c -3.819395
  4. d -2.255729
  5. e 24.000000
  6. dtype: float64

Seriesndarray 不同的地方在于,Series 的操作默认是使用 index 的值进行对齐的,而不是相对位置:

In [21]:

  1. s[1:] + s[:-1]

Out[21]:

  1. a NaN
  2. b 0.100413
  3. c -3.819395
  4. d -2.255729
  5. e NaN
  6. dtype: float64

对于上面两个不能完全对齐的 Series,结果的 index 是两者 index 的并集,同时不能对齐的部分当作缺失值处理。

Name 属性¶

可以在定义时指定 name 属性:

In [22]:

  1. s = pd.Series(np.random.randn(5), name='something')
  2. s.name

Out[22]:

  1. 'something'

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/12-pandas/12.02-series-in-pandas.ipynb