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. The basic method to create a Series is to call:

Series 是一维标记数组,能够包含任意类型数据

>>> s = pd.Series(data, index=index)

Here, data can be many different things: 

  • a Python dict
  • an ndarray
  • a scalar value (like 5)

The passed index is a list of axis labels. Thus, this separates into a few cases depending on what data is: From ndarray


If data is an ndarray, index must be the same length as data. If no index is passed, one will be created having values [0, …, len(data) – 1].


如果数据是n维数组,那么index的长度与数据的长度一致,如果没有定义index则会自增产生相应数据【0  len(data)-1

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: s = pd.Series(np.random.randn(5), index=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’])

In [4]: s


a  0.2941

b  0.2869

c  1.70

d   -0.2126

e    0.2696

dtype: float64

In [5]: s.index


Index([‘a’, ‘b’, ‘c’, ‘d’, ‘e’], dtype=’object’)

In [6]: pd.Series(np.random.randn(5))

0  -0.4531
1  -1.8215
2  -0.1263
3  -0.1533
4  0.4055

dtype: float64

Note: Starting in v0.8.0, pandas supports non-unique index values. If an operation that does not support duplicate index values is attempted, an exception will be raised at that time. The reason for being lazy is nearly all performance- based (there are many instances in computations, like parts of GroupBy, where the index is not used).

From dict

If data is a dict, if index is passed the values in data corresponding to the labels in the index will be pulled out.

Otherwise, an index will be constructed from the sorted keys of the dict, if possible.


Note: NaN(not a number)is the standard missing data marker used in pandas
From scalar value If data is a scalar value, an index must be provided. The value will be repeated to match the
length of index


In [7]: d = {‘a’ : 0., ‘b’ : 1., ‘c’ : 2.}

In [8]: pd.Series(d)


a  0.0

b  1.0

c  2.0

dtype: float64

In [9]: pd.Series(d, index=[‘b’, ‘c’, ‘d’, ‘a’])


b  1.0

c  2.0

d  NaN

a    0.0

dtype: float64

In [10]: pd.Series(5., index=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’])


a  5.0

b  5.0

c  5.0

Series 类似于n维数组

Series acts very similarly to a ndarray, and is a valid argument to most NumPy functions. However, things like slicing also slice the index.

In [11]: s[0]
Out[11]: 0.29413876297575337

In [12]: s[:3]


a  0.2941

b  0.2869

c  1.7098

dtype: float64

In [13]: s[s > s.median()]

a 0.2941
c 1.7098

dtype: float64

In [14]: s[[4, 3, 1]]

e   0.2696

d  -0.2126

b   0.2869

dtype: float64

In [15]: np.exp(s)

a  1.3420

b  1.3323

c  5.5276

d  0.8085

e  1.3094

dtype: float64


A Series is like a fixed-size dict in that you can get and set values by index label:

In [16]: s[‘a’]
Out[16]: 0.29413876297575337

In [17]: s[‘e’] = 12.

In [18]: s


a   0.2941

b   0.2869

c   1.7098

d   -0.2126

e   12.0000

dtype: float64

In [19]: ‘e’ in s



In [20]: ‘f’ in s

If a label is not contained, an exception is raised:

Using the get method, a missing label will return None or specified default:

See also the section on attribute access.


When doing data analysis, as with raw NumPy arrays looping through Series value-by-value is usually not necessary. Series can also be passed into most NumPy methods expecting an ndarray.


In [23]: s + s







In [21]: s.get(‘f’)

s[‘f’] KeyError: ‘f’

In [22]: s.get(‘f’, np.nan)

Out[22]: nan

A key difference between Series and ndarray is that operations between Series automatically align the data based on label. Thus, you can write computations without giving consideration to whether the Series involved have the same labels.


The result of an operation between unaligned Series will have the union of the indexes involved. If a label is not found in one Series or the other, the result will be marked as missing NaN. Being able to write code without doing any explicit data alignment grants immense freedom and flexibility in interactive data analysis and research. The integrated data alignment features of the pandas data structures set pandas apart from the majority of related tools for working with labeled data.


Note: In general, we chose to make the default result of operations between differently indexed objects yield the union of the indexes in order to avoid loss of information. Having an index label, though the data is missing, is typically important information as part of a computation. You of course have the option of dropping labels with missing data via the dropna function.

Name attribute


Series can also have a name attribute:
In [27]: s = pd.Series(np.random.randn(5), name=‘something’)

In [28]: s


0   -0.181904

1   -1.087438

2    0.432270

3    1.408268

4    0.123985

Name: something, dtype: float64

In [29]:


In [26]: s[1:] + s[:1]


a   NAN

b   0.5739

c   3.4195

d   -0.4252

e   NAN

dtype: float64

版权声明:本站原创文章,由 admin 2017-08-11发表,共计4437字。