drop the last 2 rows in pandas df in python

You can use the iloc method to select all rows except the last two and assign the result back to the dataframe:

main.py
df = df.iloc[:-2]
18 chars
2 lines

This will select all rows from the start of the dataframe up to the second last row. These rows are then assigned back to the dataframe, effectively dropping the last two rows from the dataframe.

related categories

gistlibby LogSnag