- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain how series data structure in Python can be created using dictionary and explicit index values?
Let us understand how series data structure can be created using dictionary, as well as specifying the index values, i.e., customized index values to the series.
Dictionary is a Python data structure that has a mapping kind of structure- a key,value pair.
Example
import pandas as pd my_data = {'ab' : 11., 'mn' : 15., 'gh' : 28., 'kl' : 45.} my_index = ['ab', 'mn' ,'gh','kl'] my_series = pd.Series(my_data, index = my_index) print("This is series data structure created using dictionary and specifying index values") print(my_series)
Output
This is series data structure created using dictionary and specifying index values ab 11.0 mn 15.0 gh 28.0 kl 45.0 dtype: float64
Explanation
- The required libraries are imported, and given alias names for ease of use.
- A dictionary data structure is created, and key-value pairs are defined in it.
- Next, customized index values are stored in a list.
- These are the same values as that of ‘key’ values in the dictionary.
- It is then printed on the console.
What happens if the values in the index are greater than the values in the dictionary?
Let us see what happens when the values in the index are greater than the values in the dictionary.
Example
import pandas as pd my_data = {'ab' : 11., 'mn' : 15., 'gh' : 28., 'kl' : 45.} my_index = ['ab', 'mn' ,'gh','kl', 'wq', 'az'] my_series = pd.Series(my_data, index = my_index) print("This is series data structure created using dictionary and specifying index values") print(my_series)
Output
This is series data structure created using dictionary and specifying index values ab 11.0 mn 15.0 gh 28.0 kl 45.0 wq NaN az NaN dtype: float64
Explanation
The required libraries are imported, and given alias names for ease of use.
A dictionary data structure is created, and key-value pairs are defined in it.
Next, a greater number customized index values in comparison to elements in the dictionary are stored in a list.
It is then printed on the console.
It can be seen that the remaining values in the index value are given values ‘NaN’, indicating ‘Not a Number’.