Menu

Creating pandas series from dictionary

Written by MachineLearningPlus | 5 min read

Pandas Series is a 1-dimensional array like object which can hold data of any type. You can create a pandas series from a dictionary by passing the dictionary to the command: pandas.Series().

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.

Also Read: Creating a pandas series from other objects. 

Using the pandas.Series method

To make a series from a dictionary, simply pass the dictionary to the command pandas.Series method.
The keys of the dictionary form the index values of the series and the values of the dictionary form the values of the series.

python
import pandas as pd

# Create the data of the series as a dictionary
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}

# Create the series
ser = pd.Series(ser_data)
ser
python
A     5
B    10
C    15
D    20
E    25
dtype: int64

Ordering the Series

By default, the order of series would be the same as the keys of the dictionary.

However, you can the change the order of the values of the series using the index parameter.

Specifying a new order

You can directly specify the order of the indices as a list, and the series will be made accordingly.

python
# Ordering a series
ser_data = {'C': 10, 'A': 5, 'D': 20, 'B': 15, 'E': 25}

# Define the new order of the series
ind = ['A', 'B', 'C', 'D', 'E']

# Create the series
ser = pd.Series(ser_data, index=ind)
ser
python
A     5
B    15
C    10
D    20
E    25
dtype: int64

Using the sorted function

The sorted function is used to return an iterable object that is sorted in ascending or descending order.
You can use this function to arrange the indices of the series in ascending or descending order.

The sorted function will arrange the values in ascending order by default.

python
# Ordering a series in ascending order
ser_data = {'C': 10, 'A': 5, 'D': 20, 'B': 15, 'E': 25}

# Create the series
ser = pd.Series(ser_data)

print('Initial ordering of the series:')
print(ser)
print('\n')

# ser_data.keys() is used to access the array of keys of the dictionary
asc_ser = pd.Series(ser_data, index=sorted(ser_data.keys()))

print('Series after arranging the indices in ascending order:')
print(asc_ser)
python
Initial ordering of the series:
C    10
A     5
D    20
B    15
E    25
dtype: int64


Series after arranging the indices in ascending order:
A     5
B    15
C    10
D    20
E    25
dtype: int64

For sorting the series in descending order, pass the boolean value True to the parameter reverse of the sorted function

python
# Ordering a series in descending order
ser_data = {'C': 10, 'A': 5, 'D': 20, 'B': 15, 'E': 25}

# Create the series
ser = pd.Series(ser_data)

print('Initial ordering of the series:')
print(ser)
print('\n')

# Pass the boolean value True to sort the series in the descending order
desc_ser = pd.Series(ser_data, index=sorted(ser_data.keys(), reverse=True))

print('Series after arranging the indices in descending order:')
print(desc_ser)
python
Initial ordering of the series:
C    10
A     5
D    20
B    15
E    25
dtype: int64


Series after arranging the indices in descending order:
E    25
D    20
C    10
B    15
A     5
dtype: int64

Subsetting the dictionary to form the series with specific values

You can subset the values of a dictionary by using the index parameter. Specify the list of key values of the dictionary whose values you wish to add to the series.

python
# Subsetting the dictionary

# Create the data of the series as a dictionory
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}

# Specify the keys which are to be used to make the series
ser = pd.Series(ser_data, index=['B', 'C', 'D'])
ser
python
B    10
C    15
D    20
dtype: int64

Using a user-defined index having excess indices

If you pass a list of index values which has more labels than the number of values in the dictionary then the values of the excess labels will be considered to NaN.

python
# Create the data of the series as a dictionory
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}

# Pass a list of index labels of length greater than the length of the dictionary
ind = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

# Create the series
ser = pd.Series(ser_data, index=ind)
ser
python
A     5.0
B    10.0
C    15.0
D    20.0
E    25.0
F     NaN
G     NaN
dtype: float64

Practical Tips

  • If you pass a label to the index parameter which is not present as a key in the dictionary then the value of that index will be assumed to NaN.
python
# Subsetting the dictionary
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}

# Specify the keys which are to be used to make the series
ser = pd.Series(ser_data, index=['B', 'C', 'H'])
ser
python
B    10.0
C    15.0
H     NaN
dtype: float64
  • If a duplicate index label is passed to the index parameter then the value of that corresponding key will also be duplicated.
python
# Subsetting the dictionary
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}

# Specify the keys which are to be used to make the series
ser = pd.Series(ser_data, index=['A', 'B', 'C', 'D', 'E', 'A'])
ser
python
A     5
B    10
C    15
D    20
E    25
A     5
dtype: int64

Test Your Knowledge

Q1: Duplicate values are not allowed as index values in a series. True or False?

Answer:

Answer: False. Duplicate values can be used as index labels in the series.

Q2: How are the index labels of the series decided if the index parameter is not specified?

Answer:

Answer: The keys of the dictionary form the index of the series.

Use the following dictionary to answer the following questions. data={'q':10, 'w':20, 'e':30, 'r':40, 't':50, 'y':60}

Q3: Make a series where the order of the labels is reversed.

Answer:

Answer: pandas.Series(data,index=['y','t','r','e','w','q'])

Q4: Make a series which contains the values only of the keys ‘q’, ‘r’ and ‘y’

Answer:

Answer: pandas.Series(data, index=['q', 'r', 'y'])

Q5: Make a series where the values are arranged in descending order

Answer:

Answer: pandas.Series(data,index=sorted(data.keys(),reverse=True))

The article was contributed by Shreyansh B and Shri Varsheni

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master Pandas — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science