Skip to content
C

Problem 4: Row Selection with loc and iloc

Easypython

Problem Description

Write a Python program that creates a DataFrame of at least 5 rows, then demonstrates the difference between .loc[] and .iloc[] by printing the row with index label 2 using .loc[], and the third row (position) using .iloc[].

Input

A DataFrame with at least 5 rows and a default integer index.

Output

Two printed rows: one selected using .loc[2], and one selected using .iloc[2].

Constraints

  • Use the default DataFrame index (0, 1, 2, 3, 4, ...).

Example Input

text
{"name": ["A", "B", "C", "D", "E"], "marks": [10, 20, 30, 40, 50]}

Example Output

text
Using loc[2]: name C marks 30 Using iloc[2]: name C marks 30

Concepts Covered

.loc[], .iloc[], DataFrame indexing.

Input (stdin)

Output

Run your code to see output here...