Selecting Columns and Rows
Selecting in pandas means pulling out the specific columns or rows you care about from a DataFrame using square-bracket indexing — the everyday way you narrow a table down to just the data you need.
Learn Selecting Columns and Rows in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Master single-column vs multi-column selection, row slicing, and the attribute-access shortcut — the foundation of every analysis you'll do.
To grab one column, put its name in square brackets after the DataFrame: df["age"] . This returns a Series — a one-dimensional labeled array, basically a single column with its index attached.
Let's build a small DataFrame from a dictionary and pull out one column:
To select more than one column, pass a list of column names inside the brackets. Because you pass a list, you end up with double brackets : df[["name", "city"]] . The result is a DataFrame , not a Series.
You also control column order this way — the columns appear in the order you list them, which is handy for reshaping output.
Square brackets behave differently when you pass a slice . df[0:3] selects rows by position (0, 1, 2) — just like slicing a Python list. The end index is exclusive .
Pandas lets you read a column as if it were an attribute: df.age instead of df["age"] . It's a nice shortcut while exploring, but it has real limits.
Build a DataFrame of three movies, then practice every selection style you learned.
Lesson complete — you can carve up any DataFrame!
You now know that single brackets give a Series, a list of names gives a DataFrame, and a slice selects rows. You also know when the handy df.col shortcut breaks down.
🚀 Up next: Filtering with Boolean Masks — select rows based on conditions, not just position.
Practice quiz
What type does df['a'] return for a single column?
- A Series
- A DataFrame
- A list
- A dict
Answer: A Series. Single-bracket access of one column returns a Series.
What does df[['a']] (double brackets) return?
- A Series
- A DataFrame with one column
- A NumPy array
- A scalar value
Answer: A DataFrame with one column. Passing a list of names returns a DataFrame, even for one column.
How do you select columns 'a' and 'c' together?
- a
- c
Pass a list of names: df[['a', 'c']].
Which accessor selects by LABEL?
loc is label-based; iloc is integer-position based.
Which accessor selects by INTEGER POSITION?
iloc uses integer positions, e.g. df.iloc[0, 1].
What does df.loc[df['a'] > 1] do?
- Sorts by column a
- Keeps rows where column a exceeds 1
- Drops column a
- Renames a
Answer: Keeps rows where column a exceeds 1. A boolean mask inside loc filters rows by condition.
How do you select a single cell at row 'r', column 'b' by label?
- r
df.loc['r', 'b'] returns the scalar at that label intersection.
What does df.iloc[0:2] return?
- The first column
- Every other row
- Nothing
- The first two rows
Answer: The first two rows. Integer slicing 0:2 selects rows at positions 0 and 1.
Which is the recommended way to set a value to avoid the chained-assignment warning?
- col
Answer: col. Use a single .loc call so pandas writes to the original frame.
What does df.filter(like='date') select?
- Columns whose name contains 'date'
- Rows equal to 'date'
- The first date column only
- Cells matching 'date'
Answer: Columns whose name contains 'date'. filter(like=...) keeps labels that contain the substring.