English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Pandas Series

Basic Operations of Pandas Series

pandas.Series

The structure of Series is as follows:

pandas.Series(data, index, dtype, copy)

The parameters of the constructor are as follows-

data: Data can take various forms, such as ndarray, list, constant index: The index values must be unique and hashable, and the length must be the same as the data. If no index is passed, the default is np.arange(n). dtype: dtype is used for data type. If None, the data type will be inferred copy: copy data. Default is False

Series can be created with various inputs, such as

Array Dict Scalar value or constant

Create an empty Series

 >>> # Import pandas dependency package and alias
 >>> import pandas as pd
 >>> s = pd.Series()
 >>> print(s)
 Series([], dtype: float64)

Create Series from ndarray

If the data is ndarray, the passed index must have the same length. If no index is passed, the default index will be range(n), where n is the array length, i.e., [0,1,2,3…。Range (len(array))-1]。

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 import numpy as np
 data = np.array(['a','b','c','d'])
 s = pd.Series(data)
 print(s)

Running Result:

 0 a
 1 b
 2 c
 3 d
 dtype: object

We did not pass any index, so by default, it assigns the index range from 0 to len(data)-1,即0到3。

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 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)

Running Result:

 100 a
 101 b
 102 c
 103 d
 dtype: object

We passed the index values here. Now, we can see the custom index values in the output.

Create Series from a dictionary

Dictionaries can be passed as input. If the index is not specified, all dictionary keys are used to build the index in sorted order. If the index is passed, the corresponding index label data will be pulled out.

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 import numpy as np
 data = {'a' : 0., 'b' : 1, 'c' : 2.}
 s = pd.Series(data)
 print(s)

Running Result:

 a 0.0
 b 1.0
 c 2.0
 dtype: float64

Dictionary keys are used to construct the index.

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 import numpy as np
 data = {

Running Result:

 b 1.0
 c 2.0
 d NaN
 a 0.0
 dtype: float64

The order of the index is maintained, and missing elements are filled with NaN (Not a Number).

Create Series from a scalar

If the data is a scalar value, an index must be provided. This value will be repeated to match the length of the index

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 import numpy as np
 s = pd.Series(5, index=[0, 1, 2, 3)
 print(s)

Running Result:

 
 0 5
 1 5
 2 5
 3 5
 dtype: int64

Access data from a Series with a location

Data in Series can be accessed like accessing ndarray.
Retrieve the first element. It is well known that the counting of arrays starts from zero, which means the first element is stored at the zeroth position, and so on.

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 s = pd.Series([1,2,3,4,5], index = ['a', 'b', 'c', 'd', 'e'])
 # Retrieve the first data
 print s[0]

Running Result:

1

Retrieve the first three elements in the Series. If inserted before, all items from this index will be extracted. If using two parameters (separated by a colon), the items between the two indices (excluding the stop index)

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 s = pd.Series([1,2,3,4,5], index = ['a', 'b', 'c', 'd', 'e'])
 # Retrieve the first3elements
 print s[:3]

Running Result:

 a 1
 b 2
 c 3
 dtype: int64

Retrieve the last three elements.

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 s = pd.Series([1,2,3,4,5], index = ['a', 'b', 'c', 'd', 'e'])
 # Retrieve the last three elements
 print s[-3:]

Running Result:

 c 3
 d 4
 e 5
 dtype: int64

Retrieve data using labels (indices)

Series is like a fixed-size dictionary and can get and set values through index labels.
Retrieve a single element using index label values.

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 s = pd.Series([1,2,3,4,5], index = [

Running Result:

 1

Retrieve multiple elements using index label values list.

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 s = pd.Series([1,2,3,4,5], index = [

Running Result:

 
 a 1
 c 3
 d 4
 dtype: int64

An exception will be raised if the label is not included.

 # Filename: pandas.py
 # author by: www.oldtoolbag.com 
 # Import pandas dependency package and alias
 import pandas as pd
 s = pd.Series([1,2,3,4,5], index = ['a', 'b', 'c', 'd', 'e'])
 # Retrieve multiple elements
 print(s['f'])

Running Result:

   ...
 KeyError: 'f'