Rows and columns with loc
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
Use `.loc` to select the employees in the Engineering department, keeping only the `name` and `years` columns (in that order).
What this exercise teaches
emp.loc[rows, columns] selects both at once. The row part is a boolean mask for the department, and the column part is a list of labels, so the result keeps the original index of each engineer.
A hint if you get stuck
loc takes two parts separated by a comma: which rows (a mask works) and which columns (a list of names).
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