Selecting Rows and Columns
Learn how to select specific rows and columns in a Pandas DataFrame so you can focus on the parts of your dataset that matter for each analysis.
Skill Level: Beginner
Prerequisites: Exploring a Dataset
Estimated Time: 25 minutes
Story Time
The StudentsPerformance dataset contains many columns, but most analysis questions focus on just a few of them.[file:42]
For example, if you only want to study mathematics scores or compare all exam scores, you do not need every column in the DataFrame. Learning how to select specific rows and columns helps you work with just the information you need.
In this lesson, you will learn how to select columns and rows using Pandas.
What You’ll Learn
By the end of this lesson, you will be able to:
- Select one or more columns from a DataFrame.
- Select rows using labels and positions.
- Use
.loc[]and.iloc[]. - Extract subsets of a dataset for analysis.
Why This Topic Matters
Most real analyses focus on subsets of the data.
You might want only exam scores, only students who took a test preparation course, or only records from a certain range of rows. If you can select specific rows and columns, you can:
- simplify your view of the data,
- reduce distraction from unrelated variables, and
- prepare focused datasets for each question.
Analysis Question
Suppose we want to answer:
What are the mathematics scores of all students?
To answer this question, we only need one column from the DataFrame.
Selecting a Single Column
Use square brackets with the column name:
df["math score"]
Example output:
0 72
1 69
2 90
...
Because only one column is selected, the result is a Series.
Selecting Multiple Columns
To compare all exam scores, select several columns at once:
df[["math score", "reading score", "writing score"]]
Example output:
| Math | Reading | Writing |
|---|---|---|
| 72 | 72 | 74 |
| 69 | 90 | 88 |
| 90 | 95 | 93 |
Notice the double square brackets: one set for the DataFrame selection, and one set for the list of column names.
Selecting Rows by Position with iloc
Use iloc[] when you want to select rows by their integer positions:
df.iloc
This returns the first row.
To select multiple rows:
df.iloc[0:5]
This returns rows from position 0 up to, but not including, position 5 (the first five rows).
Selecting Rows and Columns Together with iloc
You can select rows and columns at the same time:
df.iloc[0:5, 0:3]
This means:
- rows from position 0 to 4 (first five rows)
- columns from position 0 to 2 (first three columns)
Selecting Rows by Label with loc
Use loc[] when you want to select rows by their labels (index values):
df.loc
This selects the row whose index label is 0.
To select multiple rows:
df.loc[0:4]
With loc, both the start and end labels are included.
Selecting Specific Columns with loc
You can also select columns by name using loc:
df.loc[:, ["gender", "math score"]]
This means:
:→ all rows["gender", "math score"]→ only the gender and math score columns
Example output:
| Gender | Math Score |
|---|---|
| Female | 72 |
| Female | 69 |
| Male | 90 |
Difference Between loc and iloc
| Function | Uses |
|---|---|
loc[] | Labels (index, column names) |
iloc[] | Integer positions |
In practice:
- use
loc[]when working with labels you recognize, - use
iloc[]when you want to slice by numeric positions.
Worked Example
Question:
Display only gender and mathematics score.
df[["gender", "math score"]]
Question:
Display the first ten students.
df.iloc[:10]
Question:
Display the first five students with only gender and reading score.
df.loc[:4, ["gender", "reading score"]]
These examples show how row and column selection can be combined.
Practice in the Notebook
After completing the activity in Google Colab, mark it as complete below.
Using the StudentsPerformance dataset in Colab, perform the following tasks:
- Display only the mathematics score.
- Display mathematics and reading scores.
- Display the first ten students.
- Display the last ten students.
- Display only gender and writing score.
Try each selection in a separate code cell and observe the outputs.
Practice Exercise
Answer the following questions using Python:
- Display only the parental level of education.
- Display gender, lunch, and mathematics score.
- Display the first fifteen students.
- Display rows 50–60.
- Display only reading and writing scores for the first twenty students.
Write the code for each task and run it to confirm your selections.
Independent Exercise
Suppose a university administrator wants to review only:
- gender
- test preparation course
- mathematics score
Write code to:
- Display only these three columns.
- Display the first twenty students with these columns.
This is a realistic example of preparing a focused view for a specific audience.
Common Mistakes
Using Single Brackets for Multiple Columns
Incorrect:
df["math score", "reading score"]
Correct:
df[["math score", "reading score"]]
Multiple column names must be provided as a list inside the brackets.
Confusing loc and iloc
Remember:
loc[]uses labels (index values and column names).iloc[]uses integer positions.
Forgetting That Indexing Starts at Zero
df.iloc
returns the first row, not the second. Pandas uses zero‑based indexing.
Key Takeaways
In this lesson, you learned:
- How to select one or more columns from a DataFrame.
- How to select rows using
loc[]andiloc[]. - How to combine row and column selections.
- How to extract subsets of a dataset for focused analysis.
Self Evaluation
Check Your Understanding
1. What does df["math score"] return?
2. How do you select multiple columns: math, reading, and writing scores?
3. Which command selects the first five rows by position?
4. Which command uses labels to select all rows but only gender and math score?
5. What is the main difference between loc[] and iloc[]?
Continue Your Journey
Next, you will learn Filtering Data, where you’ll use conditions to select rows that meet specific criteria (such as students with scores above a certain threshold).