how to drop two last rows in python

To drop the last two rows of a pandas DataFrame, you can use the DataFrame.drop() method and pass the index of the last two rows. Here's an example:

main.py
import pandas as pd

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

# drop the last two rows
df = df.drop(df.tail(2).index)

# print the updated DataFrame
print(df)
205 chars
11 lines

The tail() method returns the last two rows, and the index attribute returns their index. The drop() method then removes these rows from the DataFrame.

related categories

gistlibby LogSnag