how to get x last values of a dataframe in python

To get the last 'x' rows of a pandas dataframe, you can use the tail method. The argument passed to tail is the number of rows you want to select. Here is an example where we select the last three rows:

main.py
import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [6, 7, 8, 9, 10]})

# Select the last 3 rows of the dataframe
last_three_rows = df.tail(3)

# Output the selection
print(last_three_rows)
233 chars
11 lines

This will output:

main.py
   A   B
2  3   8
3  4   9
4  5  10
36 chars
5 lines

related categories

gistlibby LogSnag