Series ( Series
) is capable of storing any type of data (integer, string, floating point, Python object, etc.) of the one-dimensional array of tags. Index referred to as the axis labels.
pandas.Series
Pandas series can be created using the constructor -
pandas.Series( data, index, dtype, copy)
parameter | description |
---|---|
data |
Data take various forms, such ndarray as: list , ,constants |
index |
Index must be unique and hash, the data length of the same. The default np.arange(n) if no index is passed. |
dtype |
dtype The type of data. If not, we will infer data types |
copy |
Copy the data, by default false . |
You can create a series using a variety of inputs, such as:
- Array
- dictionary
- Scalar or constant
Create an empty series
Creating a basic series is a series of empty.
import pandas as pd s = pd.Series() print(s)
Output:
Series([], dtype: float64)
Create a series from ndarray
If the data is ndarray
, the index of the transmission must have the same length. If the index value is not passed, then the default index range ( n
), which n
is the length of the array, i.e. [0,1,2,3…. range(len(array))-1] - 1]
.
Example 1: do not pass any index, so by default, it is assigned from 0
the len(data)-1
index, namely: 0
to 3
.
import pandas as pd import numpy as np data = np.array(['a','b','c','d']) s = pd.Series(data) print(s)
Output:
0 a
1 b
2 c
3 d
dtype: object
Example 2: pass the index value. Now you can see in the output index value custom .
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)
Output:
100 a
101 b
102 c
103 d dtype: object
Create a series from a dictionary
Dictionary ( dict
) can be passed as an input, if the index is not specified, configured to press the sort order to obtain key index dictionary . If you pass the index, the index value corresponding to the label data will be pulled out.
Example 1
import pandas as pd data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data) print(s)
输出结果:
a 0.0
b 1.0
c 2.0
dtype: float64
注意 - 字典键用于构建索引。
示例2
import pandas as pd data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data,index=['b','c','d','a']) print(s)
输出结果:
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
注意观察 - 索引顺序保持不变,缺少的元素使用NaN(不是数字)填充。
从标量创建一个系列
如果数据是标量值,则必须提供索引。将重复该值以匹配索引的长度。
import pandas as pd 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']) print(s[0])
输出结果:
1
示例-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'