Question:medium

A CSV file named customer.csv contains records of customers. The first row of the file represents the header. Sample data from the file is shown below:

Name, Age, City
Rudra,17,Kolkata
Divya,19,Dehradun
Nabeel,22,Lucknow

Write a Python program to import data from the CSV file into a Pandas DataFrame named DF and display the last two rows.

Show Hint

Read the file with pd.read_csv('customer.csv') into DF, then display the last two rows with DF.tail(2).
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Break the job into two actions:
Action one is to load the file into a DataFrame. Action two is to print its last two rows. The import of Pandas comes before both.

Step 2: Load the file:
The header is in the first row, so no extra setting is needed.
import pandas as pd
DF = pd.read_csv('customer.csv')

The columns of DF are Name, Age and City.

Step 3: Pick the last two rows:
One way is DF.tail(2).
Another way is slicing by position with iloc. The slice -2: starts two rows from the end and goes to the last row.
print(DF.iloc[-2:])

Both ways were run and both print the same two rows.

Step 4: Result on the sample file:
     Name  Age      City
1   Divya   19  Dehradun
2  Nabeel   22   Lucknow


Final Answer:
The program is import pandas as pd, DF = pd.read_csv('customer.csv'), print(DF.tail(2)).
Was this answer helpful?
0