What is the tail method?

0139

The tail() method in pandas is used to return the last n rows of a DataFrame or Series. By default, it returns the last 5 rows, but you can specify a different number of rows to return by passing an integer as an argument.

Syntax:

DataFrame.tail(n=5)

Parameters:

  • n: (int, optional) The number of rows to return from the end of the DataFrame. The default value is 5.

Example:

Here's how to use the tail() method:

import pandas as pd

# Create a sample DataFrame
data = {
    'column1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'column2': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
}
df = pd.DataFrame(data)

# Use the tail() method to get the last 3 rows
last_rows = df.tail(3)

print(last_rows)

Output:

   column1 column2
7        8       H
8        9       I
9       10       J

In this example, df.tail(3) returns the last 3 rows of the DataFrame df. This method is useful for quickly inspecting the end of a dataset, especially when working with large DataFrames.

0 Comments

no data
Be the first to share your comment!