.tail()
Anonymous contributor
Published Jul 8, 2024
Contribute to Docs
In Pandas, .tail()
is a method that returns the last n rows of a DataFrame
. By default, it returns the last 5 rows, but the number of rows can be adjusted by passing an integer argument to the method.
Syntax
df.tail(n=5)
df
: The PandasDataFrame
on which the method is called.n
: The number of rows to return from the end of theDataFrame
.
Example
The example below demonstrates the use of the .tail()
method:
import pandas as pd# Create a DataFramedf = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6], 'B': [7, 8, 9, 10, 11, 12]})# Get the last 3 rows of the DataFramelast_three_rows = df.tail(3)# Print the last 3 rowsprint(last_three_rows)
The output of the code above will be:
A B3 4 104 5 115 6 12
Codebyte Example
The codebyte example below shows how the .tail()
method works:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.