How to convert a dictionary to a Pandas series?
Let’s discuss how to covert a dictionary into pandas series in Python. A series is a one-dimensional labeled array which can contain any type of data i.e. integer, float, string, python objects, etc. while dictionary is an unordered collection of key : value pairs. We use series() function of pandas library to convert a dictionary into series by passing the dictionary as an argument.
Let’s see some examples:
Example 1: We pass the name of dictionary as an argument in series() function. The order of output will be same as of dictionary.
Example 2: We pass the name of dictionary and a different order of index. The order of output will be same as the order we passed in the argument.
Example 3: In the above example the length of index list was same as the number of keys in the dictionary. What happens if they are not equal let’s see with the help of an example.
So It’s assigning NaN value to that corresponding index.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course