Open In App
Related Articles

Creating a Pandas Series from Dictionary

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

A Pandas Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It has to be remembered that, unlike Python lists, a Series will always contain data of the same type. Let’s see how to create a Pandas Series from Python Dictionary.

Pandas Series from Dictionary

In this article, you will learn about the different methods of configuring the pandas.Series() command to make a pandas series from a dictionary followed by a few practical tips for using them.

Using Series() method without index parameter

In this case, dictionary keys are taken in a sorted order to construct the index.

Example 1: Dictionary keys are given in sorted order.

Python3

# import the pandas lib as pd
import pandas as pd
 
# create a dictionary
dictionary = {'A': 10, 'B': 20, 'C': 30}
 
# create a series
series = pd.Series(dictionary)
 
print(series)

                    
Output:
A    10
B    20
C    30
dtype: int64

Example 2: Dictionary keys are given in unsorted order.

Python3

# import the pandas lib as pd
import pandas as pd
 
# create a dictionary
dictionary = {'D': 10, 'B': 20, 'C': 30}
 
# create a series
series = pd.Series(dictionary)
 
print(series)

                    
Output:
B    20
C    30
D    10
dtype: int64

Using Series() method with index parameter

In this case, the values in data corresponding to the labels in the index will be assigned. 

Example 1: An index list is passed of the same length as the number of keys present in the dictionary. 

Python3

# import the pandas lib as pd
import pandas as pd
 
# create a dictionary
dictionary = {'A': 50, 'B': 10, 'C': 80}
 
# create a series
series = pd.Series(dictionary, index=['B', 'C', 'A'])
 
print(series)

                    
Output:
B    10
C    80
A    50
dtype: int64

Example 2: An index list is passed of greater length than the number of keys present in the dictionary, In this case, Index order is persisted and the missing element is filled with NaN (Not a Number). 

Python3

# import the pandas lib as pd
import pandas as pd
 
# create a dictionary
dictionary = {'A': 50, 'B': 10, 'C': 80}
 
# create a series
series = pd.Series(dictionary, index=['B', 'C', 'D', 'A'])
 
print(series)

                    
Output:
B    10
C    80
D   NaN
A    50
dtype: float64


Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads