- Related Questions & Answers
- C Programming for sum of the series 0.6, 0.06, 0.006, 0.0006, …to n terms
- Sum of the first N terms of the series 5,12, 23, 38…. in C Programming
- Sum of the first N terms of the series 2,10, 30, 68,…. in C programming
- Sum of the first N terms of the series 2, 6, 12, 20, 30…. in c programming
- Number of elements a particular tag contains in JavaScript?
- Dynamically add elements in ListView in android?
- Can we add null elements to a Set in Java?
- Count frequencies of all elements in array in Python using collections module
- Count distinct elements in an array in Python
- Capitalize first letter of a column in Pandas dataframe
- Apply uppercase to a column in Pandas dataframe
- intersection_update() in Python to find common elements in n arrays
- Apply function to every row in a Pandas DataFrame
- Add a row at top in pandas DataFrame
- Absolute and Relative frequency in Pandas

- 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

Advertisements