Python

Python – How to Create Dictionary using Pandas Series

In this post, you will learn about one of the important Pandas fundamental data structure namely Series and how it can be used as a dictionary. It will be useful for beginner data scientist to understand the concept of Pandas Series object. 

A dictionary is a structure that maps arbitrary keys to a set of arbitrary values.

Pandas Series is a one-dimensional array of indexed data. It can be created using a list or an array. Pandas Series can be thought of as a special case of Python dictionary. It is a structure which maps typed keys to a set of typed values.

Here are the three different ways in which a dictionary can be created using Series object:

Series like one-dimensional Numpy Array

data = pd.Series(data=[85, 65, 92, 44]
Fig 1. Pandas Series with default numeric indices similar to Numpy one-dimensional array

In the above Series object, the indices default from 0 to 3. One can access values using syntax such as data[0] is 85, data[3] is 44. The values and index can be printed using commands such as data.values and data.index.

It may look like the Series object is basically interchangeable with a one-dimensional NumPy array. The essential difference is the presence of the index: while the Numpy Array has an implicitly defined integer index used to access the values, the Pandas Series has an explicitly defined index associated with the values.

Series with explicitly defined Index with values of any type

The following is another manner in which a dictionary from Series can be created:

data = pd.Series(data=[85, 65, 92, 44], index=['Mathematics', 'English', 'Science', 'Hindi'])
Fig 2. Pandas series with explicitly defined indices

In the above Series object, you would see an explicitly defined index. This explicit index definition gives the Series bject additional capabilities. The index need not be an integer. It can consist of values of any desired type. The index must be a hashable type and need not be unique. The object supports both integer- and label-based indexing

Series with Explicitly defined Index with values of any type – II

Here is another manner in which Pandas Series object can be created as a dictionary:

data = pd.Series(data={'Mathematics': 82, 'Science': 92, 'English': 64})
Fig 3. Pandas series with explicitly defined indices
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Recent Posts

Machine Learning Lifecycle: Data to Deployment Example

Last updated: 12th May 2024 In this blog, we get an overview of the machine…

8 mins ago

Autoencoder vs Variational Autoencoder (VAE): Differences, Example

Last updated: 12th May, 2024 In the world of generative AI models, autoencoders (AE) and…

22 mins ago

Linear Regression T-test: Formula, Example

Last updated: 7th May, 2024 Linear regression is a popular statistical method used to model…

5 days ago

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

1 week ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

1 week ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

1 week ago