Pandas: Create Series from list in python

How to create series from list in Python ?

In this article we will get to know about how we will convert list to a series using Pandas.

Series class provides a constructor in python.

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

Where,

  • data : It is array-like and iterable sequence. It adds the elements in this iterable as values in the Series.
  • index : It is array-like and iterable sequence. It adds the elements in this iterable as indexes in the Series.
  • dtype : It represents the data type of the output series.

Now, we will create series class constructor to create a Pandas Series object from a list. So, let’s start exploring the topic.

Creating Pandas Series from a list :

Let’s take an example where we will convert a list into Pandas series object by passing the list in Series class constructor.

Let’s see how it is actually implemented.

#Program :


import pandas as sc

# List for strings
list_words = ['alexa', 'siri', 'try', 'sometime', 'why', 'it']

# To create a series object from string list
series_objs = sc.Series(list_words)

print('New series object: ')
print(series_objs)
Output:
New Series Objetct:
0      alexa
1      siri
2    try
3     sometime
4     why
5     it
dtype: object

Creating Pandas Series from two lists :

Let’s take a scenario where we want to have only some specific indices in Series object, so in that case we would pass another list of same size to Series class constructor as the index argument.

Let’s see how it is actually implemented.

#Program :


import pandas as sc

# List for values
list_words = ['alexa', 'siri', 'please', 'try', 'sometime', 'it']

# List for index
index_tags = ['i', 'ii', 'iii', 'iv', 'v', 'vi']

# To create a series from two list i.e. one for value, one for index
series_objs = sc.Series(list_words, index=index_tags)

print('New series object: ')
print(series_objs)
Output:
New Series Object:
i      alexa
ii      siri
iii    please
iv    try
v    sometime
vi    it
dtype: object

By default the index values start from 0 to N-1 (N= No. of elements in Series object).

Creating Pandas Series object from a list but with dissimilar datatype :

Let we want to form a Series object from a list of integer, but the items should be stored as strings inside Series object i.e. here we would convert list into Pandas Series object by converting integers into string. To get the output we should pass dtype argument in Series Constructor.

Let’s see how it is actually implemented.

#Program :


import pandas as sc

# List for integers
list_nums = [101, 100, 153, 36, 58]

# To create a series with list of different type i.e. str
series_objs = sc.Series(list_nums, index= ['i', 'ii', 'iii', 'iv', 'v'],dtype=str)

print('New Series Object:')
print(series_objs)
Output:
i     101
ii    100
iii   153
iv    36
v    58
dtype: object

Converting a heterogeneous list to Pandas Series object :

Let’s take a example of heterogenous list where if we don’t provide any dtype argument in Series constructor, then all the items will be converted to str type.

Let’s see how it is actually implemented.

#Program :


import pandas as sc

# List for mixed datatypes
list_mixed = ['some',99,'will','be',10.57,'yes']

series_objs = sc.Series(list_mixed,index= ['i', 'ii', 'iii', 'iv', 'v'])

print(series_objs)
Output:
i    some
ii    99
iii   will
iv   be
v    10.57
vi    yes
dtype: object

Converting a booli list (Bool type) to Pandas Series object :

Let’s take an example where we would create Series object from booli list of boolean type.

Let’s see how it is actually implemented.

#Program :

import pandas as sc

booli_list = [False, True, False, False, True]

# Convert a booli list to Series object of bool data type.
series_objs = sc.Series(booli_list,index=['i', 'ii', 'iii', 'iv', 'v'])

print('New Series Object:')
print(series_objs)
Output:
Contents of the Series Object:
a     False
b    True
c    False
d    False
e     True
dtype: bool