SoFunction
Updated on 2025-05-06

Summary of the use of pandas DataFrame tail

Pandas2.2 DataFrame

Indexing, iteration

method describe
([n]) Used to return the first few lines of the DataFrame
Methods to quickly access and modify individual values ​​in DataFrame
Methods to quickly access and modify individual values ​​in DataFrame
Used to access and modify data in a DataFrame based on tags (row labels and column labels)
Used to access and modify data in a DataFrame based on integer positions (row and column numbers)
(loc, column, value[, …]) Used to insert a new column at the specified location of the DataFrame
DataFrame.iter() Column name used to iterate over DataFrame
() Column names and column data used to iterate over DataFrame
() Returns the column name of the DataFrame
() Used for iterating DataFrame
([index, name]) Used for iterating DataFrame
(item) Used to delete a specified column from a DataFrame
([n]) Used to return the last of the DataFramenOK

()

([n])Method is used to return the last of the DataFramenOK. If not specifiedn, the last 5 lines are returned by default.

parameter

  • n: Optional parameter, indicating the number of rows to be returned, default is 5.

Return value

  • Return to the end of DataFramenRoad, type is

Example

Suppose we have a DataFrame as follows:

import pandas as pd

data = {
    'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'B': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
}

df = (data)
print("Original DataFrame:")
print(df)

Output:

Original DataFrame:
    A   B
0   1  11
1   2  12
2   3  13
3   4  14
4   5  15
5   6  16
6   7  17
7   8  18
8   9  19
9  10  20

usetailThe method returns the last 3 lines:

last_three_rows = (3)
print("\nLast 3 lines:")
print(last_three_rows)

Output:

Last 3 lines:
    A   B
7   8  18
8   9  19
9  10  20

If not specifiedn, the last 5 lines are returned by default:

last_five_rows = ()
print("\nThe last 5 lines by default:")
print(last_five_rows)

Output:

The last 5 lines are defaulted:
    A   B
5   6  16
6   7  17
7   8  18
8   9  19
9  10  20

You can see,tailMethods can easily obtain the last few lines of data of DataFrame.

This is the end of this article about the use of pandas DataFrame tail. For more related pandas DataFrame tail content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!