Python Practice › Pandas Programs › Program 66
Remove the last n rows
Write a pandas program to remove the last n rows.
Output
name age 0 Alex 30 1 John 27
Approach
iloc[:-n].
Solution
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})print(df.iloc[:-2])Output
name age 0 Alex 30 1 John 27
Learn the concept
New to this topic? Read the lesson, then come back.