If you’re learning Pandas, you’ve probably heard about Series — one of the most fundamental data structures in the library. In previous lessons or tutorials, you might have created a Series from a list. But did you know that you can also create a Pandas Series from a dictionary?
This approach is incredibly useful for mapping keys to values, custom indexing, and understanding how Pandas handles labeled data. In this post, we’ll explore how to create a Pandas Series using a dictionary, what’s happening behind the scenes, and how you can use it effectively in your data analysis projects.
A Pandas Series is a one-dimensional labeled array that can hold any data type — integers, floats, strings, Booleans, or even Python objects. Each value in a Series is associated with a label known as an index.
Think of it as a Python list with labels — or a dictionary with order.
Here’s the best part:
A Series combines the best features of both Python lists and Python dictionaries:
Like lists, Series maintain the order of elements.
Like dictionaries, Series have key-value mappings (labels and values).
This combination makes Series incredibly powerful for data manipulation.
Let’s start with a simple example.
First, import Pandas:
Now, let’s create a Python dictionary representing some data — for example, types of sushi and their colors:
To create a Pandas Series from this dictionary, simply use the pd.Series() constructor:
Output:
Let’s break down what’s going on in this example:
The keys from the dictionary ("Salmon", "Tuna", "Eel") become the index labels in the Series.
The values ("Orange", "Red", "Brown") become the data elements of the Series.
The dtype at the bottom (object) shows the data type of the values.
Unlike a regular Python dictionary, the Pandas Series:
Maintains the order of elements.
Supports vectorized operations.
Provides access by both label and position.
Once your Series is created, you can access elements in several ways.
Output:
Output:
Both methods work — which makes Pandas Series even more flexible than native Python data structures.
You can also pass a custom index to your Series.
For instance, suppose you only want to include specific keys from your dictionary:
Output:
Notice how Pandas filtered the data to only include the specified labels.
If a label in your index does not exist in the dictionary, Pandas will return NaN (Not a Number):
Output:
This shows how Pandas gracefully handles missing data.
Let’s take a more practical example — mapping product names to their prices.
Output:
Now, you can use this Series just like a mini database:
Here are some reasons why creating a Series from a dictionary is powerful:
Labeled Indexing: Automatically uses dictionary keys as index labels.
Order Preservation: Maintains order of elements (unlike regular Python dictionaries before version 3.7).
Flexible Access: Access data by both label and position.
Handling Missing Data: Automatically inserts NaN for missing keys.
Vectorized Operations: Perform arithmetic and comparisons easily.
Integration with DataFrames: Series can be used as DataFrame columns.
Readable Output: Data is displayed in an easy-to-understand column format.
Let’s say you have numerical data in your dictionary:
Output:
Now you can perform operations easily:
Output:
Because Series are mutable, you can easily update values by label:
Output:
You can also combine two Series objects that share similar indexes:
Output:
This is a big reason Pandas is preferred over plain Python — simple arithmetic works directly on structured data.
The index in a Pandas Series is the set of labels that identify each data point.
Even when you create a Series from a dictionary, the index is automatically generated from the keys.
You can view or modify it like this:
Output:
You can even rename the index or convert it to a list:
You can easily convert a Series back into a dictionary or other structures:
This flexibility allows you to move between Pandas and pure Python structures easily.
A Pandas Series is a labeled one-dimensional array.
When created from a dictionary, keys become index labels, and values become data.
Pandas keeps the data ordered and allows access by both labels and position.
You can perform powerful vectorized operations directly on Series.
Series seamlessly integrate into larger DataFrame structures.