How to delete all the rows of the DataFrame except the first row and the last row

Question:

I would like to delete all rows of my DataFrame except the first and the last one regardless of its size

Input
1
2
3
4
5
.
.
999999999

Expected output:

1
999999999
Asked By: Jasmine Scott

||

Answers:

You can use iloc

import pandas as pd


data = {
    "Input": [1, 2, 3, 4, 5, 6, 7, 8]
}
df = pd.DataFrame(data)
print(f"{df}n")


df = df.iloc[[0, -1]].reset_index(drop=True)
print(df)

   Input
0      1
1      8
Answered By: Jason Baker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.