How can I drop the first row in a Pandas DataFrame using two different methods?

How can I drop the first row in a Pandas DataFrame using two different methods?

The process of removing the first row in a Pandas DataFrame can be achieved using two different methods. The first method involves using the “drop” function with the “index” parameter set to 0, which will drop the first row based on its index position. The second method involves using the “iloc” function with the slicing notation [1:], which will select all rows starting from the second row and reassign them to the DataFrame. Both methods effectively remove the first row from the DataFrame and can be used depending on the specific needs and preferences of the user.

Drop First Row in Pandas DataFrame (2 Methods)


You can use one of the following methods to drop the first row in a pandas DataFrame:

Method 1: Use drop

df.drop(index=df.index[0], axis=0, inplace=True)

Method 2: Use iloc

df = df.iloc[1: , :]

Each method produces the same result.

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

	team	position assists rebounds
0	A	G	 5	 11
1	A	G	 7	 8
2	A	F	 7	 10
3	A	F	 9	 6
4	B	G	 12	 6
5	B	G	 9	 5
6	B	F	 9	 9
7	B	F	 4	 12

Method 1: Use drop

The following code shows how to use the drop() function to drop the first row of the pandas DataFrame:

#drop first row of DataFrame
df.drop(index=df.index[0], axis=0, inplace=True) 

#view updated DataFrame
df

	team	position assists rebounds
1	A	G	 7	 8
2	A	F	 7	 10
3	A	F	 9	 6
4	B	G	 12	 6
5	B	G	 9	 5
6	B	F	 9	 9
7	B	F	 4	 12

Notice that the first row has been removed from the DataFrame.

Also note that we must use inplace=True for the row to be removed in the original DataFrame.

Method 2: Use iloc

The following code shows how to use the iloc function to drop the first row of the pandas DataFrame:

#drop first row of DataFrame
df = df.iloc[1: , :]

#view updated DataFrame
df

	team	position assists rebounds
1	A	G	 7	 8
2	A	F	 7	 10
3	A	F	 9	 6
4	B	G	 12	 6
5	B	G	 9	 5
6	B	F	 9	 9
7	B	F	 4	 12

Notice that the first row has been removed from the DataFrame.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Cite this article

stats writer (2024). How can I drop the first row in a Pandas DataFrame using two different methods?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-drop-the-first-row-in-a-pandas-dataframe-using-two-different-methods/

stats writer. "How can I drop the first row in a Pandas DataFrame using two different methods?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-drop-the-first-row-in-a-pandas-dataframe-using-two-different-methods/.

stats writer. "How can I drop the first row in a Pandas DataFrame using two different methods?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-drop-the-first-row-in-a-pandas-dataframe-using-two-different-methods/.

stats writer (2024) 'How can I drop the first row in a Pandas DataFrame using two different methods?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-drop-the-first-row-in-a-pandas-dataframe-using-two-different-methods/.

[1] stats writer, "How can I drop the first row in a Pandas DataFrame using two different methods?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I drop the first row in a Pandas DataFrame using two different methods?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top