- 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
How to get few rows from a Series in Pandas?
Indexing in pandas Series is used to access elements from a series, in the same way, we can also get a set of values from a series object using the slicing technique. Nothing but we can retrieve the subset from a pandas Series object. This process is similar to the NumPy array slicing.
Retrieving some set of elements can be achieved by defining the start and endpoint index values of a series.
Example
# importing required packages import pandas as pd import numpy as np # creating pandas Series object series = pd.Series(np.random.rand(10)) print(series) print('
Retrieve first 3 elements') # accessing elements by using index values print(series[0:3]) print('
Retrieve 3 elements from middle') # accessing elements by using index values print(series[4:7])
Explanation
In this example, we have created a Series object with positional index values, and the data is random values generated by using the NumPy random module.
Here, we have retrieved a set of rows from our series object by defining the start and end values with the series name “series[start:end]”.
Output
0 0.445380 1 0.644535 2 0.961340 3 0.242597 4 0.272414 5 0.126234 6 0.579381 7 0.358938 8 0.542832 9 0.476023 dtype: float64 Retrieve first 3 elements 0 0.445380 1 0.644535 2 0.961340 dtype: float64 Retrieve 3 elements from middle 4 0.272414 5 0.126234 6 0.579381 dtype: float64
Output
The first part of the above block is representing the entire series object and second part is sub set values sliced by defining index range from 0 to 3 (series[0:3]), in the same way we have retrieved an another sub set by defining index range between 4 to 7 (series[4:7]) which is represented in last part of the above code block.
Example
We can also slice labele indexed series object, This following example will explain how to slice a series having labele indexes.
# importing required packages import pandas as pd # creating pandas Series object series = pd.Series({'B':'black', 'W':'white','R':'red', 'Bl':'blue','G':'green'}) print(series) print('
Retrieve first 2 elements') # accessing elements by using index values print(series[0:2]) print('
Retrieve elements by using label index') # accessing elements by using label index print(series['W':'G'])
Explanation
In this above example, we have created a series object with label indexes and we have applied the slicing using positional index values as well as label index values.
Accessed the 2 elements from the series by specifying position index from 0 to 2 (series[0:2]). In the same way, we have specified the label indexes also (like series[‘W’:’G’]).
Output
B black W white R red Bl blue G green dtype: object Retrieve first 2 elements B black W white dtype: object Retrieve elements by using label index W white R red Bl blue G green dtype: object
The above block is the output of an entire series object and some few rows accessed by using both positional indexing and label indexing on our label indexed series object.
- Related Articles
- How to drop duplicate rows in pandas series?
- How to get the final rows of a time series data using pandas series.last() method?
- How to remove rows in a Pandas series with duplicate indices?
- Python Pandas - Filtering few rows from a DataFrame on the basis of sum
- How to Get the values from the pandas series between a specific time?
- How to remove first few rows from each group in R?
- How to remove NaN from a Pandas Series?
- How to get the nth percentile of a Pandas series?
- How to remove last few rows from an R data frame?
- How to create a Pandas series from a python dictionary?
- How to create a series from a list using Pandas?
- Python Pandas - How to select multiple rows from a DataFrame
- How to plot a bar graph in Matplotlib from a Pandas series?
- How to get the length, size, and shape of a series in Pandas?
- How to get the rows by using pandas series.first() method?
