Pandas DataFrame - Delete rows before specific index
Pandas DataFrame - Delete rows before Specific Index
To delete rows before a specific index from a DataFrame, call DataFrame.truncate() function and pass the index value for the before
parameter.
Syntax
The syntax to delete the rows before the index x
from DataFrame df
is
df.truncate(before=x)
Examples
1. Delete rows in DataFrame present before index=3
In this example, we take a DataFrame with five rows, and delete the rows present before the index 3
using DataFrame.truncate() function.
Python Program
import pandas as pd
df = pd.DataFrame({'name':['a', 'b', 'c', 'd', 'e'],
'age' :[20, 21, 20, 19, 21]})
result = df.truncate(before=3)
print(result)
Output
name age
3 d 19
4 e 21
Summary
In this Pandas Tutorial, we learned how to delete the rows present before specific index from a DataFrame.