Pandas: Create Series from dictionary in python

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

In Pandas, the Series class provide a constructor,

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

It accepts these arguments,

  • data: array-like, Iterable sequence. All items in this iterable sequence will be added as values in the Series.
  • index: array-like, Iterable sequence. All values in this iterable sequence will be added as indices 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 dictionary in python.

Create a Pandas Series from dict in python

We can pass the dictionary to the Series class Constructor i.e. Series(). It will return a new Series object and all the keys in the dictionary will become the indices of the Series object, whereas all the values from the key-value pairs in the dictionary will become the values of the Series object.

For example,

import pandas as pd

# Dictionary of string and int
char_dict = {
    'C': 56,
    "A": 23,
    'D': 43,
    'E': 78,
    'B': 11
}

# Convert a dictionary to a Pandas Series object.
# dict keys will be index of Series &
# values of dict will become values in Series.
series_obj = pd.Series(char_dict)

print('Contents of Pandas Series: ')
print(series_obj)

Output:

Contents of Pandas Series: 
C    56
A    23
D    43
E    78
B    11
dtype: int64

A new Series object is created from the dictionary with the following data,

  • The index of this Series object contains the keys of the dictionary char_dict.
  • Values in the series object are all values from the key-value pairs of char_dict.

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

In the previous example when we converted a dictionary to a Pandas series object, then the order of indices & values in Series object is the same as the order of keys & values in the dictionary.

But what if we want Series index & values in some other order? For that we need to pass the index list as a separate argument in the Series class constructor i.e.

import pandas as pd

# Dictionary of string and int
char_dict = {
    'C': 56,
    "A": 23,
    'D': 43,
    'E': 78,
    'B': 11
}

# Create Series from dict, but pass the index list separately
series_obj = pd.Series(char_dict,
                       index=['E', 'D', 'C', 'B', 'A'])

print('Contents of Pandas Series: ')
print(series_obj)

Output

Contents of Pandas Series: 
E    78
D    43
C    56
B    11
A    23
dtype: int64

As the index list contains the same items as the keys of the dictionary, but in a different order. So Series object will be created from the dictionary’s key-value pairs but the order of items in the Series will be based on the order of items in the index list argument.

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

As we have seen in the previous examples, if we pass a dictionary as the only argument in the Series constructor, then a Series object will be created from all the items in the dictionary.

But what if we want to have only specific key-value pairs from dictionary to the Series object. For that, along with the dictionary, we can also pass the index argument in the Series constructor, but items in the index list will be less than the keys in the dictionary.

For example,

import pandas as pd

# Dictionary of string and int
char_dict = {
    'C': 56,
    "A": 23,
    'D': 43,
    'E': 78,
    'B': 11
}

# Items in index list is less than the keys in dictionary
series_obj = pd.Series(char_dict,
                       index=['E', 'D', 'C'])

print('Contents of Pandas Series: ')
print(series_obj)

Output:

Contents of Pandas Series: 
E    78
D    43
C    56
dtype: int64

As index argument we passed a list of 3 items only i.e. ‘E’, ‘D’ & ‘C’. So, Series was created from these keys & their values only, all other key-value pairs from the dictionary were just skipped.

Convert dictionary to Pandas Series object with some extra indexes

If we provide a big list of indexes along with dictionary in Series class constructor i.e. items in the list are more than the keys in the dictionary, then all the extra indexes will have value NaN.

For example,

import pandas as pd

# Dictionary of string and int
char_dict = {
    'C': 56,
    "A": 23,
    'D': 43,
    'E': 78,
    'B': 11
}

# Items in index list are greater than the keys in dictionary
series_obj = pd.Series(char_dict,
                       index=['G', 'F', 'E', 'D', 'C', 'B', 'A'])

print('Contents of Pandas Series: ')
print(series_obj)

Output:

G     NaN
F     NaN
E    78.0
D    43.0
C    56.0
B    11.0
A    23.0
dtype: float64

For items which are in index list but not in dictionary keys, their value in Series is NaN.

So, this is how we can create a Pandas Series object from a dictionary in python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top