- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

Pandas series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The elements of a pandas series can be accessed using various methods.

Let's first create a pandas series and then access it's elements.

A panadas series is created by supplying data in various forms like ndarray, list, constants and the index values which must be unique and hashable. An example is given below.

import pandas as pd s = pd.Series([11,8,6,14,25],index = ['a','b','c','d','e']) print s

Running the above code gives us the following result −

a 11 b 8 c 6 d 14 e 25 dtype: int64

We can access the data elements of a series by using various methods. We will continue to use the series created above to demonstrate the various methods of accessing.

The first element is at the index 0 position. So it is accessed by mentioning the index value in the series. We can use both 0 or the custom index to fetch the value.

import pandas as pd s = pd.Series([11,8,6,14,25],index = ['a','b','c','d','e']) print s[0] print s['a']

Running the above code gives us the following result −

11 11

In a similar manner as above we get the first three elements by using the : value in front of the index value of 3 or the appropriate custom index value.

import pandas as pd s = pd.Series([11,8,6,14,25],index = ['a','b','c','d','e']) print s[:3] print s[:'c']

Running the above code gives us the following result −

a 11 b 8 c 6 dtype: int64 a 11 b 8 c 6 dtype: int64

In a similar manner as above, we get the first three elements by using the: value at the end of the index value of 3 with a negative sign or the appropriate custom index value.

import pandas as pd s = pd.Series([11,8,6,14,25],index = ['a','b','c','d','e']) print s[-3:] print s['c':]

Running the above code gives us the following result −

c 6 d 14 e 25 dtype: int64 c 6 d 14 e 25 dtype: int64

In this case, we use the custom index values to access non-sequential elements of the series.

import pandas as pd s = pd.Series([11,8,6,14,25],index = ['a','b','c','d','e']) print s[['c','b','e']]

Running the above code gives us the following result −

c 6 b 8 e 25 dtype: int64

- Related Questions & Answers
- How to append elements to a Pandas series?
- Print the mean of a Pandas series
- How to sort a Pandas Series?
- Print the standard deviation of Pandas series
- Accessing Hash Elements in Perl
- Accessing Array Elements in Perl
- How to get the nth percentile of a Pandas series?
- Finding the length of words in a given Pandas series
- Accessing all elements at given Python list of indexes
- Combining two Series into a DataFrame in Pandas
- How to calculate the frequency of each item in a Pandas series?
- Python program to compare two Pandas series
- How to replace NaN values by Zeroes in a column of a Pandas Series?
- Write a Python code to concatenate two Pandas series into a single series without repeating the index
- Python Pandas - Select final periods of time series data based on a date offset

Advertisements