- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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
What happens if the specified index is not present in the series Python Pandas?
When the index values are customized, they are accessed using series_name[‘index_value’]. The ‘index_value’ passed to series is tried to be matched to the original series. If it is found, that corresponding data is also displayed on the console.
When the index that is tried to be accessed is not present in the series, it throws an error. It has been shown below.
import pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = ['ab', 'mn' ,'gh','kl', 'wq', 'az'] my_series = pd.Series(my_data, index = my_index) print("The series contains following elements") print(my_series) print("Accessing elements using customized index") print(my_series['mm'])
The series contains following elements ab 34 mn 56 gh 78 kl 90 wq 123 az 45 dtype: int64 Accessing elements using customized index Traceback (most recent call last): KeyError: 'mm'
The required libraries are imported, and given alias names for ease of use.
A list of data values is created, that is later passed as a parameter to the ‘Series’ function present in the ‘pandas’ library
Next, customized index values (that are passed as parameter later) are stored in a list.
The series is created and index list and data are passed as parameters to it.
The series is printed on the console.
Since the index values are customized, they are used to access the values in the series like series_name[‘index_name’].
It is searched for in the series but when it is not found, it throws a ‘KeyError’.
It is then printed on the console.
- Python Pandas - Return the label from the index or if not present, the previous one
- What happens if the egg is not Fertilised ?
- Python Pandas - Fill NaN values with the specified value in an Index object
- Python - Check if the Pandas Index is of the object dtype
- Python - Check if the Pandas Index is a floating type
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python Pandas - Create a Series from TimeDeltaIndex and set the index of the resulting Series
- Python Pandas - Check if the index has NaNs
- Python Pandas - Check if the index is empty with 0 elements
- Python Pandas - Create a Series with both the original index and name
- Python - Check if the Pandas Index holds categorical data
- Python Pandas - Check if the index has unique values
- Python Pandas - Check if the index has duplicate values
- What is NDIM in the pandas Series?
- Python - Check if the Pandas Index only consists of booleans