A series in Python is a kind of one dimensional array of any data type that we specified in NumPy module. The only difference you can find was, each and every value in a Python series is associated with the index. The default index value is from 0 to n-1 or you can specify your own indexes.

## Python Series Examples

The following long list of examples helps you understand this Python Series, how to access them, alter the items so on. This is a simple Python Series example.

import pandas as pd from pandas import Series arr = Series([15, 35, 55, 75]) print(arr)

**OUTPUT**

As you can see from the above screenshot, Series has assigned the default index values from 0 to 3 (4 – 1).

### Python Series Values and Index

The Python Series has two attributes such as values and index. You can use these attributes to get the information about the series.

**values:**This will return the array of actual data or elements in a series.**index:**As the name suggest, this object returns the index values.

import pandas as pd from pandas import Series arr = Series([12, 32, 52, -15, 122]) print(arr) print('\nValues in this Array : ',arr.values) print('Index Values of this Array : ',arr.index)

**OUTPUT**

### Python Series Index

You can use the index attribute to create your own index or assign your own index to the series of data. This is very useful and best way to identify the data for future analysis.

import pandas as pd from pandas import Series arr = Series([25, 50, 75, 100, 125], index = [2, 4, 6, 8, 10]) print(arr) print('\nValues in this Array : ',arr.values) print('Index Values of this Array : ',arr.index)

**OUTPUT**

Let me use Alphabets as an index values

import pandas as pd from pandas import Series arr = Series([2, 33, 66, 70, 15], index = ['a', 'e', 'i', 'o', 'u']) arr arr.index

If you use an external file to save this code then use the below shown code

import pandas as pd from pandas import Series arr = Series([2, 33, 66, 70, 15], index = ['a', 'e', 'i', 'o', 'u']) print(arr) print('\nValues in this Array : ',arr.values) print('Index Values of this Array : ',arr.index)

**OUTPUT**

### Create a Python Series using arange

You can also use the arange function available in NumPy module to create a series of consecutive numbers from 0 to n-1.

import pandas as pd Import numpy as np from pandas import Series arr = Series(np.arange(6)) arr # print(arr) # Here, we are assigning the index names. arr1 = Series(np.arange(6), index = ['a', 'b', 'c', 'd', 'e', 'f']) arr 1# print(arr1)

**OUTPUT**

### Alter Python Series Index

The Series index function is not only allows you display the index items, you can alter it as well. This example changes the existing index items and places the integer values as the index.

import pandas as pd from pandas import Series new_arr = Series([2, 33, 66, 70, 15], index = ['a', 'e', 'i', 'o', 'u']) print(new_arr) print('\nValues in this Array : ',new_arr.values) print('Index Values of this Array : ',new_arr.index) print(' ') # Assigning New Index Values new_arr.index = [1, 2, 3, 4, 5] print(new_arr) print('\nValues in this Array : ',new_arr.values) print('Index Values of this Array : ',new_arr.index)

**OUTPUT**

### Create a Python Series from Dictionary

Python also allows you to create a Series for a **dictionary.** When you use dictionary, values becomes series data and dictionary keys will act as index.

import pandas as pd from pandas import Series f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000} print('Dictionary Items') print(f_dict) arr = Series(f_dict) print('\nArray Items') print(arr)

**OUTPUT**

We can also change the order of the index values, while converting g a dictionary to a Series.

import pandas as pd from pandas import Series f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000} new_list = ['apples', 'cherries', 'kiwi', 'oranges'] arr = Series(f_dict, index = new_list) print('Array Items') print(arr)

**OUTPUT**

What if we add an index that doesn’t exists in the actual dictionary key, and obviously, it won’t have any value associated with it.

import pandas as pd from pandas import Series f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000} new_list = ['apples', 'banana', 'cherries', 'kiwi', 'oranges'] arr = Series(f_dict, index = new_list) print('Array Items') print(arr)

From the below screenshot, you can see, it was returning NaN as the value for that index or key

### Python Series attributes

The Series Object has an important attribute called name. You can use this attribute to assign a name for both the data and the indexes.

import pandas as pd from pandas import Series f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000} arr = Series(f_dict) print(arr) print('\nAssigning Names') arr.name = 'No of Items' arr.index.name = 'Fruits' print(arr)

**OUTPUT**

### Accessing Python Series Items

Like NumPy Arrays, we can use the index position or index number to access an item from a Series.

import pandas as pd from pandas import Series arr = Series([22, 44, 66, 88, 108]) arr

Access the Series Item at index position 0, 2 and 4.

arr[0] arr[2] arr[4]

Creating a new series or selecting a set of values from an existing one.

arr[[1, 3, 0, 4]]

**OUTPUT**

In this example, we are accessing the data in a Series with custom index values. For this, we used Alphabet characters as the index items.

import pandas as pd from pandas import Series arr = Series([2, 4, -6, 8, -10, 12], index = ['a', 'e', 'i', 'o', 'u', 'z']) arr arr['a'] arr['u'] arr[['a', 'o', 'z', 'e']]

**OUTPUT**

### Python Series in Operator

Before we perform anything, it would be nice, if we know whether the index that we are looking was exists or not. For this, we have in operator that can check and returns True, if it exists otherwise, False.

arr1 = Series([22, 33, 44, 55], index = ['a', 'b', 'c', 'd']) arr1 'b' in arr1 'c' in arr1 'f' in arr1

**OUTPUT**

Let me use the **Dictionary** as an array input, and use in operator

f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries':6000} arr2 = Series(f_dict) arr2 'kiwi' in arr2 'banana' in arr2 'oranges' in arr2

**OUTPUT**

### Python Series Nulls

Python pandas module has isnull and notnull function, which are used to identify the Null and returns a Boolean True or False. To demonstrate the same, we are using the same dictionary that we used above.

Import pandas as pd from pandas import Series dict_items = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000} f_list = ['apples', 'banana', 'cherries', 'kiwi', 'oranges'] arr = Series(dict_items, index = f_list) arr

The following statements return True, if its is NULL or NA otherwise, False

pd.isnull(arr) arr.isnull()

The below statements return False, if the value is NaN, Null or NA otherwise, True

pd.notnull(arr) arr.notnull()

**OUTPUT**

### Arithmetic Operations on Python Series

Series allows you to perform arithmetic operations on its data. You can use any of the operator to perform on all the series items. Below example shows you the same.

from pandas import Series arr = Series([2, 4, -6, 8, -7], index = ['a', 'e', 'i', 'o', 'u']) arr

Adding 3 to each and every item in a series

arr + 3

Subtracting 2 from series items

arr - 2

Multiplying each series item with 10.

arr * 10

Returning a subset of series whose values are greater than 0.

arr[arr > 0]

**OUTPUT**

### Python Series Mathematical Function

You can use the math function that are supported by the NumPy module on a series data.

from pandas import Series import numpy as np arr = Series([2, 4, -6, 8, -7], index = ['a', 'e', 'i', 'o', 'u']) arr

Calculate the Power Of E

np.exp(arr)

Absolute Positive Value

np.fabs(arr)

Square root of each item in a series

np.sqrt(arr)

**OUTPUT**