- Related Questions & Answers
- Explain how the bottom ‘n’ elements can be accessed from series data structure in Python?
- Explain the different ways in which data from a series data structure can be accessed in Python?
- Explain how series data structure in Python can be created using scalar/constant values?
- Explain how series data structure in Python can be created using dictionary and explicit index values?
- How can data be represented visually using ‘seaborn’ library in Python?
- How can the ‘Word2Vec’ algorithm be trained using Tensorflow?
- How can BeautifulSoup be used to extract ‘href’ links from a website?
- How can the ‘subplot’ function be used to create two graphs in Matplotlib Python?
- How can ‘placeholders’ in Tensorflow be used while multiplying matrices?
- How can a simple bivariate distribution be shown using ‘imshow’ in Matplotlib Python?
- How can Matplotlib be used to generate time-series data?
- Bayes’ Rule in Data Structure
- A data structure for n elements and O(1) operations?
- What are all the ways keyword ‘this’ can be used in Java?
- Explain how a dataframe structure can be created using list of dictionary values in Python?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We have previously used slicing with the help of operator ‘:’, which is used in the case of extracting top ‘n’ elements from series structure. It helps assign a range to the series elements that will later be displayed.

Let us see an example −

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) n = 3 print("Top 3 elements are :") print(my_series[:n])

The series contains following elements ab 34 mn 56 gh 78 kl 90 wq 123 az 45 dtype: int64 Top 3 elements are : ab 34 mn 56 gh 78 dtype: int64

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.

A specific range of values can be accessed from the series using indexing ‘:’ operator in Python.

The ‘:’ operator can be used between the lower range value and higher range value: [lower range : higher range].

This will include the lower range value but exclude the higher range value.

If no value is provided for lower range, it is taken as 0.

If no value is provided for higher range, it is taken as len(data structure)-1.

Here, it indicates that the lower range is 0 and higher range is 3.

It is then printed on the console.

Advertisements