The `tail()` Method
The tail()
method fetches the last few rows from a dataframe.
It also returns 5
rows by default. A custom number of rows can be specified using the n
named parameter.
Syntax
DataFrame.tail(n=5)
Example
12345678910111213141516171819import pandas as pd # Create a simple DataFrame about a bookstore books = pd.DataFrame({ 'Title': [ 'The Alchemist', '1984', 'To Kill a Mockingbird', 'The Great Gatsby', 'Moby Dick', 'Pride and Prejudice', 'War and Peace', 'The Hobbit', 'Crime and Punishment', 'Jane Eyre' ], 'Author': [ 'Paulo Coelho', 'George Orwell', 'Harper Lee', 'F. Scott Fitzgerald', 'Herman Melville', 'Jane Austen', 'Leo Tolstoy', 'J.R.R. Tolkien', 'Fyodor Dostoevsky', 'Charlotte BrontΓ«' ], 'Stock': [12, 8, 15, 7, 3, 10, 5, 9, 4, 6] }) print('Show the last 7 books in the list:') print(books.tail(n=3))
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 3
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Suggested prompts:
Ask me questions about this topic
Summarize this chapter
Show real-world examples
Awesome!
Completion rate improved to 2.7
The `tail()` Method
Swipe to show menu
The tail()
method fetches the last few rows from a dataframe.
It also returns 5
rows by default. A custom number of rows can be specified using the n
named parameter.
Syntax
DataFrame.tail(n=5)
Example
12345678910111213141516171819import pandas as pd # Create a simple DataFrame about a bookstore books = pd.DataFrame({ 'Title': [ 'The Alchemist', '1984', 'To Kill a Mockingbird', 'The Great Gatsby', 'Moby Dick', 'Pride and Prejudice', 'War and Peace', 'The Hobbit', 'Crime and Punishment', 'Jane Eyre' ], 'Author': [ 'Paulo Coelho', 'George Orwell', 'Harper Lee', 'F. Scott Fitzgerald', 'Herman Melville', 'Jane Austen', 'Leo Tolstoy', 'J.R.R. Tolkien', 'Fyodor Dostoevsky', 'Charlotte BrontΓ«' ], 'Stock': [12, 8, 15, 7, 3, 10, 5, 9, 4, 6] }) print('Show the last 7 books in the list:') print(books.tail(n=3))
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 3