In this article we will mainly discuss how to convert a list to a Series in Pandas. In details we will cover the following topics,

  • Creating a Pandas Series from a list
  • Creating a Pandas Series from two lists (one for value and another for index)
  • Create a Pandas Series from a list but with a different data type.
  • Converting a bool list to Pandas Series object.

In Pandas, Series class provide a constructor,

Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

It accepts the following arguments,

  • data: array-like, Iterable sequence. Adds the items in this iterable as values in the Series,
  • index: array-like, Iterable sequence. Adds the items in this iterable as indexes in the Series,
  • dtype: Data type of the output series.

We are going to use this Series class constructor to create a Pandas Series object from a list.

Creating a Pandas Series from a list

To convert a list to Pandas series object, we will pass the list in the Series class constructor and it will create a new Series Object,

import pandas as pd

# List of strings
list_of_words = ['at', 'is', 'this', 'the', 'why', 'how']

# Create a Series object from list
series_obj = pd.Series(list_of_words)

print('Contents of the Series Object:')
print(series_obj)

Output:

Contents of the Series Object:
0      at
1      is
2    this
3     the
4     why
5     how
dtype: object

As dtype argument was not provided, so data type of values in series was same as the data type of items in the list. Also, as we didn’t pass the index argument therefore default indices were used i.e. from 0 to N-1, where N is the number of elements in the Series object.

Create a Pandas Series from two lists

If we want to have some specific indices in the Series object, then we need to pass another list to the Series class constructor, as index argument. Like this,

import pandas as pd

list_of_words = ['at', 'is', 'this', 'the', 'why', 'how']
index_names = ['a', 'b', 'c', 'd', 'e', 'f']

# Create a series from two lists (one for values and other for index)
series_obj = pd.Series(list_of_words, index=index_names)

print('Contents of the Series Object:')
print(series_obj)

Output:

Contents of the Series Object:
a      at
b      is
c    this
d     the
e     why
f     how
dtype: object

It created a Series object from two of the given lists. Items in the list_of_words were set as values in the series object, whereas items in list  index_names were set as indexes in the Series object.

The size of both the given lists was the same. But what if we pass both lists of different sizes?

If the size index list and values list are not equal then it will raise ValueError. For example,

import pandas as pd

list_of_words = ['at', 'is', 'this', 'the', 'why', 'how']
index_names = ['a', 'b', 'c']

series_obj = pd.Series(list_of_words, index=index_names)

print(series_obj)

Error

ValueError: Length of passed values is 6, index implies 3

As the list provided in the index argument contains less elements than the list of values, therefore it raises the ValueError.

Create a Pandas Series object from a list but with different data type

In all the above examples we have seen, that if we don’t pass the dtype argument in Series constructor, then by default the type of elements in Series object will be the same as the type of items in the list.

Now suppose we have a list of integers and we want to create a Series object from this list. But items should be stored as strings inside the Series object. Basically, we want to convert integers to strings while converting a list to a pandas Series object.

For that we need to pass the dtype argument in Series class constructor,

import pandas as pd

# List of integers
list_of_nums = [11, 23, 34, 56, 67]

# Create a series from list with different data type i.e. str
series_obj = pd.Series(list_of_nums,
                       index= ['a', 'b', 'c', 'd', 'e'],
                       dtype=str)

print('Contents of the Series Object:')
print(series_obj)

Output:

a    11
b    23
c    34
d    56
e    67
dtype: object

It will Create a Series object from the items in the list, but the data type of values in Series object will be of data type which we provided as dtype argument.

Convert a heterogeneous list to Pandas Series object

What if we have a heterogeneous list i.e. all items in the list are of mixed data types. In that case, if dtype argument is not provided then all items will be converted to str type i.e. object in pandas. For example,

import pandas as pd

# List of mix data types
mix_list = [11, 'at', 'is', 55, 66, 77]

series_obj = pd.Series(mix_list,
                       index=['a', 'b', 'c', 'd', 'e', 'f'])

print(series_obj)

Output:

a    11
b    at
c    is
d    55
e    66
f    77
dtype: object

Convert a bool list to Pandas series object

Similarly we can create a Series object from a bool list i.e.

import pandas as pd

bool_list = [True, False, False, False, True]

# Convert a bool list to Series object of bool data type.
series_obj = pd.Series(bool_list,
                       index=['a', 'b', 'c', 'd', 'e'])

print('Contents of the Series Object:')
print(series_obj)

Output:

Contents of the Series Object:
a     True
b    False
c    False
d    False
e     True
dtype: bool

The data type of all the items in Series object will be bool.

So, this is how we can convert a list to a Series object in Pandas.

Join a list of 2000+ Programmers for latest Tips & Tutorials