What is the use of tail() methods in Pandas series?

PandasServer Side ProgrammingProgramming

The tail method in pandas series object is used to retrieve bottom elements from a series. And this tail method takes an integer as a parameter which is represented by variable n.

Based on that n value the pandas series tail method will return a series object with n number of bottom elements from the actual series object.

Let’s take an example and see how this tail method will work on our pandas series object.

Example

# importing required packages
import pandas as pd

# creating pandas Series object
series = pd.Series(list(range(10,100,4)))

print(series)

print('\nResult from tail() method:')

# accessing bottom elements by using til method
print(series.tail())

Explanation

Here we have created a pandas series object using python list with range function and defined the tail method to our series object like this “series.tail()”. We specified the tail method emptily so that we can get 5 bottom elements from our series object. Let’s check the output block.

Output

0   10
1   14
2   18
3   22
4   26
5   30
6   34
7   38
8   42
9   46
10  50
11  54
12  58
13  62
14  66
15  70
16  74
17  78
18  82
19  86
20  90
21  94
22  98
dtype: int64

Result from tail() method:
18  82
19  86
20  90
21  94
22  98
dtype: int64

There are two series objects in the above output block one is the output of the entire series object and the second one the output for the tail method. In the second portion, we can see the 5 bottom elements from index 18 to 22.

Example

series.tail(2)

Explanation

We can specify an integer value as a parameter to the tail method. which is used to restrict the number of output elements. So based on this parameter we can see that particular number of bottom elements of our series object.

Output

21  94
22  98
dtype: int64

These are the 2 bottom elements of the series object, those are retrieved from the tail method.

raja
Updated on 17-Nov-2021 08:16:56

Advertisements