Creating pandas series from dictionary
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.
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.
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.
For sorting the series in descending order, pass the boolean value True to the parameter reverse of the sorted function
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.
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.
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.
- If a duplicate index label is passed to the index parameter then the value of that corresponding key will also be duplicated.
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