系列(Series
)是可以保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组。轴标签统称为索引。python
Pandas系列可使用如下构造函数建立 -shell
pandas.Series( data, index, dtype, copy)。
构造函数的参数以下 -数组
编号 | 参数 | 描述 |
---|---|---|
1 | data |
数据采起各类形式,如:ndarray ,list ,constants |
2 | index |
索引值必须是惟一的和散列的,与数据的长度相同。 默认np.arange(n) 若是没有索引被传递。 |
3 | dtype |
dtype 用于数据类型。若是没有,将推断数据类型 |
4 | copy |
复制数据,默认为false 。 |
可使用各类输入建立一个系列,如 -函数
建立一个基本系列是一个空系列。code
示例对象
#import the pandas library and aliasing as pd import pandas as pd s = pd.Series() print s Python
执行上面示例代码,输出结果以下 -排序
Series([], dtype: float64)
若是数据是ndarray
,则传递的索引必须具备相同的长度。 若是没有传递索引值,那么默认的索引将是范围(n
),其中n
是数组长度,即[0,1,2,3…. range(len(array))-1] - 1]
。索引
示例1three
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = np.array(['a','b','c','d']) s = pd.Series(data) print s
执行上面示例代码,输出结果以下 -ip
0 a 1 b 2 c 3 d dtype: object
示例2
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = np.array(['a','b','c','d']) s = pd.Series(data,index=[100,101,102,103]) print s Python
执行上面示例代码,输出结果以下 -
100 a 101 b 102 c 103 d dtype: object Python
在这里传递了索引值。如今能够在输出中看到自定义的索引值
字典(dict
)能够做为输入传递,若是没有指定索引,则按排序顺序取得字典键以构造索引。 若是传递了索引,索引中与标签对应的数据中的值将被拉出。
示例2
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data) print s Python
执行上面示例代码,输出结果以下 -
a 0.0 b 1.0 c 2.0 dtype: float64 Python
注意 - 字典键用于构建索引。
示例
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data,index=['b','c','d','a']) print s Python
执行上面示例代码,输出结果以下 -
b 1.0 c 2.0 d NaN a 0.0 dtype: float64 Python
注意观察 - 索引顺序保持不变,缺乏的元素使用NaN(不是数字)填充。
若是数据是标量值,则必须提供索引。将重复该值以匹配索引的长度。
#import the pandas library and aliasing as pd import pandas as pd import numpy as np s = pd.Series(5, index=[0, 1, 2, 3]) print s
执行上面示例代码,获得如下结果 -
0 5 1 5 2 5 3 5 dtype: int64
系列中的数据可使用相似于访问ndarray
中的数据来访问。
示例-1
检索第一个元素。好比已经知道数组从零开始计数,第一个元素存储在零位置等等。
import pandas as pd s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) #retrieve the first element print s[0] Python
执行上面示例,获得如下结果 -
1 Shell
示例-2
检索系列中的前三个元素。 若是a:
被插入到其前面,则将从该索引向前的全部项目被提取。 若是使用两个参数(使用它们之间),两个索引之间的项目(不包括中止索引)。
import pandas as pd s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) #retrieve the first three element print s[:3]
执行上面示例,获得如下结果 -
a 1 b 2 c 3 dtype: int64
示例-3
检索最后三个元素,参考如下示例代码 -
import pandas as pd s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) #retrieve the last three element print s[-3:]
执行上面示例代码,获得如下结果 -
c 3 d 4 e 5 dtype: int64
一个系列就像一个固定大小的字典,能够经过索引标签获取和设置值。
示例1
使用索引标签值检索单个元素。
import pandas as pd s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) #retrieve a single element print s['a']
执行上面示例代码,获得如下结果 -
1
示例2
使用索引标签值列表检索多个元素。
import pandas as pd s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) #retrieve multiple elements print s[['a','c','d']]
执行上面示例代码,获得如下结果 -
a 1 c 3 d 4 dtype: int64
示例3
若是不包含标签,则会出现异常。
import pandas as pd s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) #retrieve multiple elements print s['f']
执行上面示例代码,获得如下结果 -
KeyError: 'f'