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

Time:10-06

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

CodePudding user response:

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
  • Related