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.
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
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.
# 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
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.
# 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)
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
# 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)
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.
# 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
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.
# 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
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.
# 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
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.
# 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
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: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