You can delete/drop the first row from the Pandas DataFrame using either drop()
, iloc[]
and tail()
methods. In this article, I will explain how to delete/drop the first row from Pandas DataFrame with examples.
drop()
method is used to remove columns or rows from DataFrame.- Use the
axis
param to specify what axis you would like to remove. By defaultaxis=0
meaning to remove rows. Useaxis=1
or columns param to remove columns. - Use
inplace=True
to remove row/column in place meaning on existing DataFrame without creating a copy.
1. Quick Examples of Dropping First Row from DataFrame
If you are in a hurry, below are some quick examples of how to drop the first row of DataFrame.
# Quick examples of dropping first row from dataframe
# Example 1: Use DataFrame.iloc[]
# To drop first row
df1 = df.iloc[1:]
# Example 2: Use DataFrame.drop() function
# To delete first row
df.drop(index=df.index[0], axis=0, inplace=True)
# Example 3: Use DataFrame.tail() method
# To drop first row
df1 = df.tail(-1)
Let’s create DataFrame using data from the Python dictionary and run the above examples to get the first row of DataFrame.
# Import pandas library
# Create DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
'Fee' :[20000,25000,22000,24000,30000],
'Duration':['30day','40days','35days','60days','55days'],
'Discount':[1000,2300,2500,2000,3000]
}
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies, index =index_labels)
print("Create DataFrame:\n", df)
Yields below output.
2. Pandas Drop First Row using iloc[]
By using DataFrame.iloc[] attribute you can drop the first row of pandas DataFrame. Actually, this function selects the particular portion of a given DataFrame based on its indices and ignores the rest of the portion. The particular portion may be a few rows or a few columns.
For example, use this syntax DataFrame.iloc[1:]
to select all rows except the first row and then assign it back to the original variable which ideally drops the first row from DataFrame. We can also get the first row of DataFrame and get the last row of DataFrame using the Pandas iloc[]
property.
# Use DataFrame.iloc[] to drop first row
df1 = df.iloc[1:]
print("The DataFrame after dropping the first row:\n", df1)
Yields below output.
3. Use DataFrame.drop() to Delete First Row
In pandas, DataFrame.drop()
function is used to drop rows from the Dataframe. To remove rows use axis=0
param to the drop()
function. Use the index param to specify which row you want to delete from the DataFrame and inplace=True
to apply the change to the existing DataFrame.
# Drop first row using drop()
df.drop(index=df.index[0], axis=0, inplace=True)
print("The DataFrame after dropping the first row:\n", df)
Yields below output.
# Output:
The DataFrame after dropping the first row:
Courses Fee Duration Discount
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
r4 pandas 24000 60days 2000
r5 Hadoop 30000 55days 3000
4. Use DataFrame.tail() Method to Drop First Row
In Python, Pandas DataFrame provides a tail()
function that is used to return the last n rows of the DataFrame. So, we can delete the first row of DataFrame by using df.tail(-1)
, it will select all rows except the first row of DataFrame.
# Use DataFrame.tail() method to drop first row
df1 = df.tail(-1)
print("The DataFrame after dropping the first row:\n", df1)
Yields the same output as above.
Frequently Asked Questions on Pandas Drop the First Row of DataFrame
You can use various methods. Two common approaches are using DataFrame.drop
or DataFrame.iloc
.
You can drop the first row in place without creating a new DataFrame using the inplace=True
parameter. Both the DataFrame.drop
method and the DataFrame.iloc
method support this parameter.
Besides the methods mentioned earlier, you can use the drop
method with the index
parameter to drop the first row based on its index.
By default, the drop
method returns a new DataFrame without modifying the original one. If you want to modify the original DataFrame in place, use the inplace=True
parameter.
There are several methods to drop the first row of a DataFrame in Pandas. In addition to using DataFrame.drop
and DataFrame.iloc
, another approach is to use the tail()
method.
If you want to drop the first row of a DataFrame based on a condition, you can use boolean indexing.
Conclusion
In this article, you have learned how to drop the first row from Pandas DataFrame using DataFrame.iloc[]
, DataFrame.drop()
, and DataFrame.tail()
functions with examples.
Happy Learning !!
Related Articles
- Sort Pandas DataFrame by Single Column
- Find Unique Values From Columns in Pandas
- Get Unique Rows in Pandas DataFrame
- Pandas Drop Last N Rows From DataFrame
- How to Get Row Numbers in Pandas DataFrame?
- Get First N Rows From Pandas DataFrame
- Pandas Drop Rows Based on Column Value
- Drop First/Last N Columns From Pandas DataFrame
- Pandas get the number of rows from DataFrame
- Pandas Difference Between loc[] vs iloc[]
- How to Convert List to Pandas Series
- Delete/Drop First N Rows From Pandas DataFrame