To drop the last 2 rows from a pandas DataFrame in Python, we can use the drop() method with axis=0 and inplace=True:
main.py40 chars2 lines
Here df.tail(2) selects the last 2 rows of the DataFrame, and df.tail(2).index retrieves their index. Finally, df.drop() removes the selected rows from the original DataFrame.
Alternatively, you can also use negative indexing with .iloc:
main.py18 chars2 lines
Here, df.iloc[:-2] selects all rows except the last 2 rows, and creates a new DataFrame that is assigned back to df.
gistlibby LogSnag