• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / pandas tail – Return Last n Rows from DataFrame

pandas tail – Return Last n Rows from DataFrame

January 21, 2022 Leave a Comment

To get the last n rows from a pandas DataFrame, you can use the pandas tail() function.

df.tail() #Default will return the last 5 rows

When working with data and designing scripts to update data, sometimes it is useful to be able to do simple checks on our data to ensure everything is populating correctly.

The pandas tail() function allows us to get the last n rows of our DataFrame. By default, n is 5, but you can change this to any valid integer.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Age': [43,23,71,49,52,37], 
      'Test_Score':[90,87,92,96,84,79]})

print(df)
# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   49          96
4   52          84
5   37          79

We can get the last 5 rows by calling tail().

print(df.tail())

# Output: 
   Age  Test_Score
1   23          87
2   71          92
3   49          96
4   52          84
5   37          79

If we only want the last 2 rows, we pass “2” to tail()

print(df.tail(2))

# Output: 
   Age  Test_Score
4   52          84
5   37          79

If you want to get the first n rows from a pandas DataFrame, you can use the pandas head() function.

Getting the Last Row from a Pandas DataFrame

To get the last row from a pandas DataFrame, we can use the pandas tail() function. All we need to do is pass “1” to tail() to get the last row.

Let’s say we have the same DataFrame from above. Getting the last row is easy, as shown below in the following Python code.

print(df.tail(1))

# Output: 
   Age  Test_Score
5   37          79

Hopefully this article has been useful for you to understand how to use the pandas tail() function to get the last n rows from a pandas DataFrame.

Other Articles You'll Also Like:

  • 1.  Check if Set Contains Element in Python
  • 2.  Using readlines() and strip() to Remove Spaces and \n from File in Python
  • 3.  Truncate String in Python with String Slicing
  • 4.  Using Python Selenium Webdriver to Refresh Page
  • 5.  Read First Line of File Using Python
  • 6.  pandas product – Get Product of Series or DataFrame Columns
  • 7.  How to Remove Vowels from a String in Python
  • 8.  Pandas Crosstab on Multiple Columns
  • 9.  Generate Random Float Between 0 and 1 Using Python
  • 10.  Flatten List of Tuples in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy