Skip to content
LearnPY
pandas

Selecting rows and columns with confidence

Use brackets, loc, iloc, boolean masks, and query to pull out exactly the rows and columns you mean.

11 min readbeginnerpandas

By the end

  • Select one column or several columns with brackets.
  • Use loc for label-based selection and iloc for position-based selection.
  • Combine conditions with boolean masks.
  • Write readable filters with query.
  • Sort results and assign new values without chained indexing.

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01Which selector uses row and column labels?
02How do you combine two boolean masks with logical AND?
03Why is chained assignment risky?

Lesson progress