logo头像
Snippet 博客主题

Pandas-Series

Python“玩”数据的利器!


Series

Series是一个能够容纳任意数据类型的带标记的一维数组。(Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index. )

官方文档

初始化

从map初始化

1
2
3
4
5
6
7
8
9
>>> import numpy as np
>>> s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
>>> s
a 1.677571
b 0.277393
c 0.945309
d 1.276764
e 0.686729
dtype: float64

从数组初始化
1
2
3
4
5
6
7
8
9
>>> data = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
... dtype=[("a", "i4"), ("b", "i4"), ("c", "i4")])
>>> s = pd.Series(data, columns=['c', 'a'])
...
>>> df3
c a
0 3 1
1 6 4
2 9 7

转化为list

1
2
>>> s.tolist()
[1.6775712053562015, 0.2773932925846383, 0.9453092483775857, 1.276764091946826, 0.6867285857318276]

转化为dict

1
2
3
4
5
6
7
8
9
10
11
>>> s = pd.Series([1, 2, 3, 4])
>>> s.to_dict()
{0: 1, 1: 2, 2: 3, 3: 4}

>>> from collections import OrderedDict, defaultdict
>>> s.to_dict(OrderedDict)
OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])

>>> dd = defaultdict(list)
>>> s.to_dict(dd)
defaultdict(<class 'list'>, {0: 1, 1: 2, 2: 3, 3: 4})

评论系统未开启,无法评论!