You can delete/drop the first two/three rows from the pandas DataFrame using either drop()
, iloc[]
and tail()
methods. In this article, I will explain how to delete/drop the first three rows from Pandas DataFrame with examples.
drop()
method is used to drop columns or rows from DataFrame.- Use
axis
param to specify what axis you would like to remove. By default axis = 0 meaning to remove rows. Use axis=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 Drop First Three Rows From DataFrame
If you are in a hurry, below are some quick examples of how to drop/delete the first two/three rows from Pandas DataFrame.
# Below are some quick examples
# 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)
Now, let’s create a DataFrame with a few rows and columns and execute some examples, and validate the results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# 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(df)
Yields below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30day 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
r4 pandas 24000 60days 2000
r5 Hadoop 30000 55days 3000
2. Use DataFrame.iloc[] to Drop 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 example, use DataFrame.iloc[3:]
to select all rows except the first three rows and then assign it back to the original variable which ideally drops the first three rows from DataFrame.
# 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)
Yields below output.
# Output:
Courses Fee Duration Discount
r4 pandas 24000 60days 2000
r5 Hadoop 30000 55days 3000
3. Use DataFrame.drop() to Delete First Three Rows
Alternatively, you can also use DataFrame.drop()
method to remove the first three rows. Use the index param to specify the first index and inplace=True
to apply the change on 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)
Yields the same output as above.
4. Use DataFrame.tail() Method to Drop the First Three Rows
You can also use DataFrame.tail(-3)
to remove the first three rows of pandas DataFrame.
# Use DataFrame.tail() method to drop first three rows
df2 = df.tail(-3)
print(df2)
Yields the same output as above.
5. Complete Example For Drop First Three Rows From 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(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)
6. Conclusion
In this article, you have learned how to drop the first three rows from Pandas DataFrame using DataFrame.iloc[]
, DataFrame.drop()
, and DataFrame.tail()
function with examples.
Happy Learning !!
Related Articles
- How to Drop Rows From Pandas DataFrame Examples
- Pandas Drop Rows by Index
- Delete Last Row From Pandas DataFrame
- Pandas – Drop List of Rows From DataFrame
- Pandas Drop Last N Rows From DataFrame
- Pandas Drop First N Rows From DataFrame
- How to drop the first row from the Pandas DataFrame
- How to drop duplicate rows from DataFrame?
- Drop Pandas rows with condition
- Pandas loc[] attribute multiple conditions
- Pandas Series loc[] attribute
- How to Slice Columns in Pandas DataFrame
- Pandas compare two DataFrames row by row
- Difference between two DataFrames