Skip to main content

Python Pandas: Access elements using Index Number

In Pandas we can access data from the series by using various methods. We can easily access the elements of series by referring to its index number. Index Number must be an integer. 

We can also perform slicing to access multiple elements from a series. Slicing Operations is perfromed on the series by using colon (:). Slicing of series is similar to list slicing . It follows the same format.

To print elements within a range use [Start Index:End Index].

Example

import pandas as pd

data = ['P','R','O','G','R','A','M','S','B','U','Z','Z']
series_data = pd.Series(data)
print(series_data[0])

Output: P

print(series_data[6])

Output: M

print(series_data[0:5])

Output:

0    P
1    R
2    O
3    G
4    R
dtype: object
print(series_data[6:])

Output:

6     M
7     S
8     B
9     U
10    Z
11    Z
dtype: object
print(series_data[-6:-1])

Output:

6     M
7     S
8     B
9     U
10    Z
dtype: object
Submitted by devanshi.srivastava on November 3, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.