Delete the first three rows of a DataFrame in Pandas

PythonServer Side ProgrammingProgramming

To delete the first three rows of a DataFrame in Pandas, we can use the iloc() method.

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
  • Print the input DataFrame, df.
  • Delete the first three rows using df.iloc[3:].
  • Print the updated DataFrame.

Example

import pandas as pd

df = pd.DataFrame(
   {
      "x": [5, 2, 7, 0, 7, 0, 5, 2],
      "y": [4, 7, 5, 1, 5, 1, 4, 7],
      "z": [9, 3, 5, 1, 5, 1, 9, 3]
   }
)

print "Input DataFrame is:\n", df
df = df.iloc[3:]
print "After deleting the first 3 rows: \n", df

Output


Input DataFrame is:
   x  y  z
0  5  4  9
1  2  7  3
2  7  5  5
3  0  1  1
4  7  5  5
5  0  1  1
6  5  4  9
7  2  7  3

After deleting the first 3 rows:
   x  y  z
3  0  1  1
4  7  5  5
5  0  1  1
6  5  4  9
7  2  7  3
raja
Published on 14-Sep-2021 13:30:13
Advertisements