Friday, November 15, 2024
Google search engine
HomeLanguagesPython | Pandas Dataframe/Series.tail() method

Python | Pandas Dataframe/Series.tail() method

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas tail() method is used to return bottom n (5 by default) rows of a data frame or series.

Syntax: Dataframe.tail(n=5)

Parameters:
n: integer value, number of rows to be returned

Return type: Dataframe with bottom n rows

To download the data set used in following example, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.

Example #1:
In this example, bottom 5 rows of data frame are returned and stored in a new variable. No parameter is passed to .tail() method since by default it is 5.




# importing pandas module
import pandas as pd
  
# making data frame
  
# calling tail() method 
# storing in new variable
data_bottom = data.tail()
  
# display
data_bottom


Output:
As shown in the output image, it can be seen that the index of returned rows is ranging from 453 to 457. Hence, last 5 rows were returned.

 
Example #2: Calling on Series with n parameter()

In this example, the .tail() method is called on series with custom input of n parameter to return bottom 12 rows of the series.




# importing pandas module
import pandas as pd
  
# making data frame
  
# number of rows to return
n = 12
  
# creating series
series = data["Salary"]
  
# returning top n rows
bottom = series.tail(n = n)
  
# display
bottom


Output:
As shown in the output image, top 12 rows ranging from 446 to 457th index position of the Salary column were returned.

RELATED ARTICLES

Most Popular

Recent Comments