Pandas: Create Series from dictionary in python

Creating Series from dictionary in python

In this article we will discuss about different ways to convert a dictionary in python to a Pandas Series object.

Series class provides a constructor in Pandas i.e

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

Where,

  • data : It represents array-like, Iterable sequence where all items in this iterable sequence will be added as values in the Series.
  • index : It represents array-like, Iterable sequence where all values in this iterable sequence will be added as indices in the Series.
  • dtype : It represents datatype of the output series.

Create a Pandas Series from dict in python :

By passing the dictionary to the Series class Constructor i.e. Series(). we can get a new Series object where all the keys in the dictionary will become the indices of the Series object, and all the values from the key-value pairs in the dictionary will converted into the values of the Series object.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 56,
    "A": 23,
    'D': 43,
    'E': 78,
    'B': 11
}
# Converting a dictionary to a Pandas Series object.
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
series_object = pd.Series(dict)
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
C  56
A  23
D  43
E  78
B  11
dtype: int64

Where the index of the series object contains the keys of the dictionary and the values of the series object contains the values of the dictionary.

Create Pandas series object from a dictionary with index in a specific order :

In the above example we observed the indices of the series object are in the same order as the keys of the dictionary. In this example we will see how to convert the dictionary into series object with some other order.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 6,
    "A": 3,
    'D': 4,
    'E': 8,
    'B': 1
}
# Creating Series from dict, but pass the index list separately
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
# But the order of indices will be some other order
series_object = pd.Series(dict,
                       index=['E', 'D', 'C', 'B', 'A'])
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
E  8
D  4
C  6
B  1
A  3
dtype: int64

Create a Pandas Series object from specific key-value pairs in a dictionary :

In above examples we saw Series object is created from all the items in the dictionary as we pass the dictionary as the only argument in the series constructor. But now we will see how we will see how we can convert specific key-value pairs from dictionary to the Series object.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 6,
    "A": 3,
    'D': 4,
    'E': 8,
    'B': 1
}
# Creating Series from dict, but pass the index list separately
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
# But here we have passed some specific key-value pairs of dictionary
series_object = pd.Series(dict,
                       index=['E', 'D', 'C'])
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
E 8
D 4
C 6
dtype: int64