Pandas常用的数据结构有两大类:series序列
和data frame数据帧
。由于panel面板
被弃用,在这里,我们就不进行介绍了。其实panel
是三维数据比较好用的数据容器。这篇文章将介绍Series
数据类型。
Series 序列
创建序列
import pandas as pd
import numpy as np
s = pd.Series(data, index=index)
这里的data
可以是Python字典,numpy的ndarray,也可以是一个实数。
从ndarray建立
In [3]: s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
In [4]: s
Out[4]:
a 0.4691
b -0.2829
c -1.5091
d -1.1356
e 1.2121
dtype: float64
In [5]: s.index
Out[5]: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
In [6]: pd.Series(np.random.randn(5))
Out[6]:
0 -0.1732
1 0.1192
2 -1.0442
3 -0.8618
4 -2.1046
dtype: float64
注意:pandas支持非唯一索引值。