How to create a series from a list using Pandas?

Pandas Series can be created in different ways, here we will see how to create a pandas Series object with a python list.

To create a pandas series we have pandas.Series() function from pandas functionalities.

Let’s take an example and create a simple pandas Series using a python list. In order to create a pandas series from the python list, firstly we need to define a python list object.


import pandas as pd

# defining a list
list_of_values = [2,89,34,78,3]

# creating series
s = pd.Series(list_of_values)


In the above code, we have imported the pandas package using the python import keyword and changed the pandas package name to ‘pd’ by using the python aliasing keyword ‘as’. Then, created a list with a total of 5 integer values.

By using the pandas Series method we have created a pandas Series object.


0   2
1  89
2  34
3  78
4   3
dtype: int64

This simple pandas Series object was created by using a list of integer values, and the index values are auto-created by the pandas Series object starting from 0-4, the elements all having the same data type int64.


Let’s take another example and create a pandas series object with named indexes, to do this we need to use the index attribute from the pandas Series method.

# defining a list
data = ['p','a','n','d','a','s']

# creating series with index values
s = pd.Series(data, index=['a','b','c','d','e','f'])

By using the index attribute from the pandas Series method we can assign labels to our series elements.

Here the variable data is holding a python list object with string elements of length 6.


a   p
b   a
c   n
d   d
e   a
f   s
dtype: object

All the elements of this pandas Series are having object data types. And the index of this above example is named labels defined by using the index attribute of the Series method.