Columns first
Most selection starts with columns. A single column name returns a Series; a list of names returns a DataFrame. Keeping that distinction straight prevents a lot of confusing shape errors.
Python
One column versus a column list
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
.loc selects by label
loc uses row and column labels. The syntax is df.loc[row_selector, column_selector]. Both selectors can be a single label, a list of labels, a slice, or a boolean Series.
Python
A label and a label-based filter
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
- sales.loc[0, "units"] selects the row labeled 0 and the column named units.
- The row selector can be a boolean Series, which keeps only the True rows.
- The column selector can be a list, which keeps only those columns.
- A slice in loc includes its ending label, unlike a normal Python slice.
.iloc selects by position
iloc ignores labels and uses integer positions, like a NumPy array. It is useful when you want the first three rows or columns regardless of what they are called. Positions are zero-based and slices exclude the stop.
Python
A row by position and a rectangular slice
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Boolean masks and query
A comparison on a column produces a boolean Series. Pass that mask inside brackets to keep the True rows. Combine conditions with & for and, | for or, and ~ for not; wrap each comparison in parentheses.
Python
Two conditions, two spellings
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
query is a readable alternative that refers to column names as if they were variables. It is handy for multi-condition filters, though ordinary boolean masks are often clearer when the conditions are built dynamically.
Sort, then assign safely
sort_values returns a new frame in the requested order. Sorting is usually the last step before displaying a result. To change values, assign to a column directly rather than chaining selection and assignment across two indexing operations.
Python
Sort a copy, then add a column to the original
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
A selection recipe
The order below keeps mistakes visible: filter rows first, then keep the columns you need, then sort for presentation. You can inspect the intermediate frame at any point instead of hiding the whole chain in one enormous expression.
Python
Filter, project, and order
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
This recipe uses a boolean mask for rows, loc with a colon to keep every row and a column list to choose fields, and sort_values to order the output. The next lesson groups rows rather than selecting them.
Practice
Make it stick
Run the tests as often as you like. Hints and the solution are available when you need them.
Exercise
Filter sales to North region rows with at least 10 units. Keep only the region and revenue columns, then sort by revenue from highest to lowest and assign the result to result.
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress