Pandasdrill

Level 1: Selecting and filtering Exercise 4 of 45 Free

Position-based slicing with iloc

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

Return the first three rows and the first two columns of `emp`, selected by position with `.iloc`.

What this exercise teaches

emp.iloc[:3, :2] takes rows at positions 0, 1 and 2 and columns at positions 0 and 1. Positions ignore the index labels, which is the key difference from loc.

A hint if you get stuck

iloc slices work like Python lists: the stop position is excluded, and counting starts at 0.

Try it now, no account needed

This exercise is free. Open it on the practice page, solve it, and get instant feedback with a hint and a worked solution after two tries.

Open this exercise

More exercises in Selecting and filtering