To drop the first three rows from a Pandas DataFrame, you can use the drop()
function with the index parameter set to a list containing the row indices you want to remove.
In this article, I will explain dropping the first three rows from DataFrame by using either drop()
, iloc[]
, or tail()
functions.
Key Points –
- The
drop()
function can be applied directly to the DataFrame object. - Slicing with
.index[:3]
selects the first three-row indices for removal. - Using
inplace=True
withindrop()
modifies the original DataFrame in place without creating a new one. - The
drop()
function provides flexibility in specifying the rows or columns to be removed based on index labels or positions, enhancing the data manipulation capabilities of Pandas DataFrames.
Quick Examples of Drop First Three Rows
Below are some quick examples of how to drop the first two/three rows from DataFrame.
# Quick examples of drop first three rows
# Use DataFrame.iloc[]
# To drop first three rows
df2 = df.iloc[3:]
# Use DataFrame.iloc[]
n = 3
df2 = df.iloc[n:]
# Use DataFrame.drop() function
# To delete first three rows
df.drop(df.head(3).index, inplace=True)
# Use DataFrame.drop() function
df.drop(df.index[:3], inplace=True)
# Use DataFrame.tail() method
# To drop first three rows
df2 = df.tail(-3)
To run some examples of dropping the first three rows from Pandas DataFrame, let’s create Pandas DataFrame using data from a dictionary.
# 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("Original DataFrame\n",df)
Yields below output.
Use DataFrame.iloc[] to Remove First Three Rows
By using DataFrame.iloc[]
you can drop the rows from DataFrame and use 3:
it to drop the first three rows. For instance, used DataFrame.iloc[3:]
to filter and select all rows except the first three, effectively removing those initial rows from the DataFrame, and then reassigning the resulting DataFrame back to the original variable.
# Use DataFrame.iloc[]
# To drop first three rows
df2 = df.iloc[3:]
print(df2)
# Use DataFrame.iloc[]
n = 3
df2 = df.iloc[n:]
print(df2)
# Output:
# Courses Fee Duration Discount
# r4 pandas 24000 60days 2000
# r5 Hadoop 30000 55days 3000
Use DataFrame.drop() to Delete First Three Rows
Alternatively, you can also utilize the DataFrame.drop()
method to eliminate the first three rows. Specify the first index using the index parameter and set inplace=True
to apply the change directly to the existing DataFrame. For instance, df.drop(df.head(3).index, inplace=True)
method.
# Use DataFrame.drop() function
# To delete first three rows
df.drop(df.head(3).index, inplace=True)
print(df)
# Use DataFrame.drop() function
df.drop(df.index[:3], inplace=True)
print(df)
# Output:
# Courses Fee Duration Discount
# r4 pandas 24000 60days 2000
# r5 Hadoop 30000 55days 3000
Use DataFrame.tail() Method to Drop First Three Rows
The DataFrame.tail()
method is typically used to retrieve the last n rows of a DataFrame. However, we can manipulate it slightly to drop the first three rows by combining it with the negative indexing feature.
# Use DataFrame.tail() method
# To drop first three rows
df2 = df.tail(-3)
print(df2)
# Output:
# Courses Fee Duration Discount
# r4 pandas 24000 60days 2000
# r5 Hadoop 30000 55days 3000
Complete Example for Drop First Three Rows
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(df)
# Use DataFrame.iloc[]
# To drop first three rows
df2 = df.iloc[3:]
print(df2)
# Use DataFrame.iloc[]
n = 3
df2 = df.iloc[n:]
print(df2)
# Use DataFrame.drop() function
# To delete first three rows
df.drop(df.head(3).index, inplace=True)
print(df)
# Use DataFrame.drop() function
df.drop(df.index[:3], inplace=True)
print(df)
# Use DataFrame.tail() method
# To drop first three rows
df2 = df.tail(-3)
print(df2)
Conclusion
In this article, you have learned about various methods for dropping the first three rows from a Pandas DataFrame. We explored techniques such as iloc[]
, drop()
, and even repurposed tail()
to achieve this task, supported by practical examples.
Happy Learning !!
Related Articles
- How to Drop Rows From DataFrame
- Pandas Drop Rows by Index
- Delete Last Row From Pandas DataFrame
- Drop List of Rows From DataFrame
- Pandas Drop Last N Rows From DataFrame
- Pandas Drop First N Rows From DataFrame
- Drop Pandas rows with condition
- Pandas loc[] attribute multiple conditions
- Pandas Series loc[] attribute
- How to Slice Columns in Pandas DataFrame