Accessing elements of a Pandas Series
Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type. An element in the series can be accessed similarly to that in an ndarray. Elements of a series can be accessed in two ways –
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
In this article, we are using “
nba.csv” file, to download the CSV, click here.
Accessing Element from Series with Position
In order to access the series element refers to the index number. Use the index operator [ ] to access an element in a series. The index must be an integer.
In order to access multiple elements from a series, we use Slice operation. Slice operation is performed on Series with the use of the colon(:). To print elements from beginning to a range use
[:Index], to print elements from end-use
[:-Index], to print elements from specific Index till the end use
[Index:], to print elements within a range, use [Start Index:End Index] and to print whole Series with the use of slicing operation, use
[:]. Further, to print the whole Series in reverse order, use
Code #1: Accessing a first element of series
Code #2: Accessing first 5 elements of Series
Code #3: Accessing last 10 elements of Series
Code #4: Accessing first 5 elements of Series in
Now we access first 5 elements of series
Accessing Element Using Label (index)
In order to access an element from series, we have to set values by index label. A Series is like a fixed-size dictionary in that you can get and set values by index label.
Code #1: Accessing a single element using index label
Code #2: Accessing a multiple element using index label
Code #3: Access multiple elements by providing label of index
Code #4: Accessing a multiple element using index label in
Now we access an multiple element using index label