mirror of
https://github.com/apachecn/ailearning.git
synced 2026-02-09 21:36:27 +08:00
416 lines
4.4 KiB
Markdown
416 lines
4.4 KiB
Markdown
# 一维数据结构:Series
|
||
|
||
In [1]:
|
||
|
||
```py
|
||
import numpy as np
|
||
import pandas as pd
|
||
|
||
```
|
||
|
||
`Series` 是一维带标记的数组结构,可以存储任意类型的数据(整数,浮点数,字符串,`Python` 对象等等)。
|
||
|
||
作为一维结构,它的索引叫做 `index`,基本调用方法为
|
||
|
||
```py
|
||
s = pd.Series(data, index=index)
|
||
```
|
||
|
||
其中,`data` 可以是以下结构:
|
||
|
||
* 字典
|
||
* `ndarray`
|
||
* 标量,例如 `5`
|
||
|
||
`index` 是一维坐标轴的索引列表。
|
||
|
||
## 从 ndarray 构建
|
||
|
||
如果 `data` 是个 `ndarray`,那么 `index` 的长度必须跟 `data` 一致:
|
||
|
||
In [2]:
|
||
|
||
```py
|
||
s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
|
||
|
||
s
|
||
|
||
```
|
||
|
||
Out[2]:
|
||
|
||
```py
|
||
a -0.032806
|
||
b 0.050207
|
||
c -1.909697
|
||
d -1.127865
|
||
e -0.073793
|
||
dtype: float64
|
||
```
|
||
|
||
查看 `index`:
|
||
|
||
In [3]:
|
||
|
||
```py
|
||
s.index
|
||
|
||
```
|
||
|
||
Out[3]:
|
||
|
||
```py
|
||
Index([u'a', u'b', u'c', u'd', u'e'], dtype='object')
|
||
```
|
||
|
||
如果 `index` 为空,那么 `index` 会使用 `[0, ..., len(data) - 1]`:
|
||
|
||
In [4]:
|
||
|
||
```py
|
||
pd.Series(np.random.randn(5))
|
||
|
||
```
|
||
|
||
Out[4]:
|
||
|
||
```py
|
||
0 -0.376233
|
||
1 -0.474349
|
||
2 1.660590
|
||
3 0.461434
|
||
4 0.190965
|
||
dtype: float64
|
||
```
|
||
|
||
## 从字典中构造
|
||
|
||
如果 `data` 是个 `dict`,如果不给定 `index`,那么 `index` 将使用 `dict` 的 `key` 排序之后的结果:
|
||
|
||
In [5]:
|
||
|
||
```py
|
||
d = {'a' : 0., 'b' : 1., 'c' : 2.}
|
||
|
||
pd.Series(d)
|
||
|
||
```
|
||
|
||
Out[5]:
|
||
|
||
```py
|
||
a 0
|
||
b 1
|
||
c 2
|
||
dtype: float64
|
||
```
|
||
|
||
如果给定了 `index`,那么将会按照 `index` 给定的值作为 `key` 从字典中读取相应的 `value`,如果 `key` 不存在,对应的值为 `NaN`(not a number, `Pandas` 中的缺失默认值):
|
||
|
||
In [6]:
|
||
|
||
```py
|
||
pd.Series(d, index=['b', 'd', 'a'])
|
||
|
||
```
|
||
|
||
Out[6]:
|
||
|
||
```py
|
||
b 1
|
||
d NaN
|
||
a 0
|
||
dtype: float64
|
||
```
|
||
|
||
## 从标量值构造
|
||
|
||
如果 `data` 是标量,那么 `index` 值必须被指定,得到一个值为 `data` 与 `index` 等长的 `Series`:
|
||
|
||
In [7]:
|
||
|
||
```py
|
||
pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])
|
||
|
||
```
|
||
|
||
Out[7]:
|
||
|
||
```py
|
||
a 5
|
||
b 5
|
||
c 5
|
||
d 5
|
||
e 5
|
||
dtype: float64
|
||
```
|
||
|
||
## 像 ndarray 一样使用 Series
|
||
|
||
In [8]:
|
||
|
||
```py
|
||
s
|
||
|
||
```
|
||
|
||
Out[8]:
|
||
|
||
```py
|
||
a -0.032806
|
||
b 0.050207
|
||
c -1.909697
|
||
d -1.127865
|
||
e -0.073793
|
||
dtype: float64
|
||
```
|
||
|
||
支持数字索引操作:
|
||
|
||
In [9]:
|
||
|
||
```py
|
||
s[0]
|
||
|
||
```
|
||
|
||
Out[9]:
|
||
|
||
```py
|
||
-0.032806330572971713
|
||
```
|
||
|
||
切片:
|
||
|
||
In [10]:
|
||
|
||
```py
|
||
s[:3]
|
||
|
||
```
|
||
|
||
Out[10]:
|
||
|
||
```py
|
||
a -0.032806
|
||
b 0.050207
|
||
c -1.909697
|
||
dtype: float64
|
||
```
|
||
|
||
`mask` 索引:
|
||
|
||
In [11]:
|
||
|
||
```py
|
||
s[s > s.median()]
|
||
|
||
```
|
||
|
||
Out[11]:
|
||
|
||
```py
|
||
a -0.032806
|
||
b 0.050207
|
||
dtype: float64
|
||
```
|
||
|
||
花式索引:
|
||
|
||
In [12]:
|
||
|
||
```py
|
||
s[[4, 3, 1]]
|
||
|
||
```
|
||
|
||
Out[12]:
|
||
|
||
```py
|
||
e -0.073793
|
||
d -1.127865
|
||
b 0.050207
|
||
dtype: float64
|
||
```
|
||
|
||
支持 `numpy` 函数:
|
||
|
||
In [13]:
|
||
|
||
```py
|
||
np.exp(s)
|
||
|
||
```
|
||
|
||
Out[13]:
|
||
|
||
```py
|
||
a 0.967726
|
||
b 1.051488
|
||
c 0.148125
|
||
d 0.323724
|
||
e 0.928864
|
||
dtype: float64
|
||
```
|
||
|
||
## 像字典一样使用 Series
|
||
|
||
也可以像字典一样使用 `Series`:
|
||
|
||
In [14]:
|
||
|
||
```py
|
||
s["a"]
|
||
|
||
```
|
||
|
||
Out[14]:
|
||
|
||
```py
|
||
-0.032806330572971713
|
||
```
|
||
|
||
修改数值:
|
||
|
||
In [15]:
|
||
|
||
```py
|
||
s["e"] = 12.
|
||
|
||
s
|
||
|
||
```
|
||
|
||
Out[15]:
|
||
|
||
```py
|
||
a -0.032806
|
||
b 0.050207
|
||
c -1.909697
|
||
d -1.127865
|
||
e 12.000000
|
||
dtype: float64
|
||
```
|
||
|
||
查询 `key`:
|
||
|
||
In [16]:
|
||
|
||
```py
|
||
"e" in s
|
||
|
||
```
|
||
|
||
Out[16]:
|
||
|
||
```py
|
||
True
|
||
```
|
||
|
||
In [17]:
|
||
|
||
```py
|
||
"f" in s
|
||
|
||
```
|
||
|
||
Out[17]:
|
||
|
||
```py
|
||
False
|
||
```
|
||
|
||
使用 `key` 索引时,如果不确定 `key` 在不在里面,可以用 `get` 方法,如果不存在返回 `None` 或者指定的默认值:
|
||
|
||
In [18]:
|
||
|
||
```py
|
||
s.get("f", np.nan)
|
||
|
||
```
|
||
|
||
Out[18]:
|
||
|
||
```py
|
||
nan
|
||
```
|
||
|
||
## 向量化操作
|
||
|
||
简单的向量操作与 `ndarray` 的表现一致:
|
||
|
||
In [19]:
|
||
|
||
```py
|
||
s + s
|
||
|
||
```
|
||
|
||
Out[19]:
|
||
|
||
```py
|
||
a -0.065613
|
||
b 0.100413
|
||
c -3.819395
|
||
d -2.255729
|
||
e 24.000000
|
||
dtype: float64
|
||
```
|
||
|
||
In [20]:
|
||
|
||
```py
|
||
s * 2
|
||
|
||
```
|
||
|
||
Out[20]:
|
||
|
||
```py
|
||
a -0.065613
|
||
b 0.100413
|
||
c -3.819395
|
||
d -2.255729
|
||
e 24.000000
|
||
dtype: float64
|
||
```
|
||
|
||
但 `Series` 和 `ndarray` 不同的地方在于,`Series` 的操作默认是使用 `index` 的值进行对齐的,而不是相对位置:
|
||
|
||
In [21]:
|
||
|
||
```py
|
||
s[1:] + s[:-1]
|
||
|
||
```
|
||
|
||
Out[21]:
|
||
|
||
```py
|
||
a NaN
|
||
b 0.100413
|
||
c -3.819395
|
||
d -2.255729
|
||
e NaN
|
||
dtype: float64
|
||
```
|
||
|
||
对于上面两个不能完全对齐的 `Series`,结果的 `index` 是两者 `index` 的并集,同时不能对齐的部分当作缺失值处理。
|
||
|
||
## Name 属性
|
||
|
||
可以在定义时指定 `name` 属性:
|
||
|
||
In [22]:
|
||
|
||
```py
|
||
s = pd.Series(np.random.randn(5), name='something')
|
||
s.name
|
||
|
||
```
|
||
|
||
Out[22]:
|
||
|
||
```py
|
||
'something'
|
||
``` |