You may use the following syntax to remove the first row/s in Pandas DataFrame:
(1) Remove the first row in a DataFrame:
df = df.iloc[1:]
(2) Remove the first n rows in a DataFrame:
df = df.iloc[n:]
Examples of Removing the First Rows in a DataFrame
Example 1: Remove the first row in a DataFrame
To start, let’s say that you created the following DataFrame that contains 5 rows:
import pandas as pd data = { "product": ["Computer", "Tablet", "Printer", "Keyboard", "Monitor"], "brand": ["AA", "BB", "CC", "DD", "EE"], "price": [1200, 350, 150, 80, 500], } df = pd.DataFrame(data) print(df)
Here is the DataFrame with the 5 rows:
product brand price
0 Computer AA 1200
1 Tablet BB 350
2 Printer CC 150
3 Keyboard DD 80
4 Monitor EE 500
Now let’s say that you want to remove the first row in the above DataFrame.
In that case, simply add df = df.iloc[1:] to the code below:
import pandas as pd data = { "product": ["Computer", "Tablet", "Printer", "Keyboard", "Monitor"], "brand": ["AA", "BB", "CC", "DD", "EE"], "price": [1200, 350, 150, 80, 500], } df = pd.DataFrame(data) df = df.iloc[1:] print(df)
You’ll notice that the first row in the original DataFrame was removed:
product brand price
1 Tablet BB 350
2 Printer CC 150
3 Keyboard DD 80
4 Monitor EE 500
Example 2: Remove the first n rows in a DataFrame
Now let’s say that you’d like to remove the first 3 rows from the original DataFrame.
Therefore, you may use the following code to remove the first 3 rows:
import pandas as pd data = { "product": ["Computer", "Tablet", "Printer", "Keyboard", "Monitor"], "brand": ["AA", "BB", "CC", "DD", "EE"], "price": [1200, 350, 150, 80, 500], } df = pd.DataFrame(data) df = df.iloc[3:] print(df)
As you can see, the first 3 rows were removed:
product brand price
3 Keyboard DD 80
4 Monitor EE 500