Selection and Indexing in Pandas
Learn how to select individual columns, access specific rows, retrieve cells, and manage DataFrame indexes using Pandas.
1. What Is Selection and Indexing?
Pandas DataFrames contain rows and columns. In practical projects, we rarely need the entire dataset every time. Sometimes we need only one column, sometimes a few rows, and sometimes one particular cell.
Selection means choosing the data we need.
Indexing means accessing data using labels or
positions. Pandas provides tools such as column names,
loc[], iloc[], and index-management methods.
Example: Student Marks Dataset
import pandas as pd
df = pd.DataFrame({
"Name": ["Samadhan", "Pradnya", "Samiksha", "Amit"],
"Age": [21, 22, 20, 23],
"City": ["Pune", "Mumbai", "Pune", "Nashik"],
"Marks": [85, 92, 78, 88]
})
print(df)
Expected output:
| Index | Name | Age | City | Marks |
|---|---|---|---|---|
| 0 | Samadhan | 21 | Pune | 85 |
| 1 | Pradnya | 22 | Mumbai | 92 |
| 2 | Samiksha | 20 | Pune | 78 |
| 3 | Amit | 23 | Nashik | 88 |
2. Understand the DataFrame Structure
A DataFrame has row labels (index) and column labels. You can use these labels or numerical positions to locate data.
Figure 1: A DataFrame uses row indexes and column labels to organize data.
3. Topics Covered in This Module
Study these three topics in sequence. Each topic has its own detailed lesson and executable examples.
Column Selection
Select one column, multiple columns, and subsets of columns using column names.
Open lesson → TOPIC 02loc[] and iloc[]
Select rows and cells by their labels or by their integer positions.
Open lesson → TOPIC 03Index Management
Set a column as the index, reset the index, and rename index labels when required.
Open lesson →4. Basic Selection Examples
Example 1: Select One Column
print(df["Name"])
Expected output:
0 Samadhan 1 Pradnya 2 Samiksha 3 Amit Name: Name, dtype: object
Selecting one column with a single pair of brackets returns a
Pandas Series.
Example 2: Select Multiple Columns
print(df[["Name", "Marks"]])
Expected output:
Name Marks 0 Samadhan 85 1 Pradnya 92 2 Samiksha 78 3 Amit 88
Selecting multiple columns requires a list of column names inside the outer brackets. The result is a DataFrame.
Example 3: Select a Row by Position
print(df.iloc[1])
Expected output:
Name Pradnya Age 22 City Mumbai Marks 92 Name: 1, dtype: object
df["Name"] selects a
column, while df.iloc[1] selects the second row
by position.
5. loc[] vs iloc[] at a Glance
The most important difference is how each method identifies a
row or column. loc[] uses labels; iloc[]
uses integer positions.
Figure 2: Use labels with loc[] and zero-based positions with iloc[].
iloc[1] means the second
row because Python positions start at zero. But
loc[1] looks for the row whose index label is
1. These can refer to different rows when custom
index labels are used.
6. Quick Reference Table
| Code | Purpose | Typical result |
|---|---|---|
df["Name"] |
Select one column | Series |
df[["Name", "Marks"]] |
Select multiple columns | DataFrame |
df.loc[1] |
Select row with index label 1 | Series for a single row |
df.iloc[1] |
Select row at position 1 | Series for a single row |
df.loc[1, "Marks"] |
Select a cell using row label and column label | Scalar value |
df.iloc[1, 3] |
Select a cell using row and column positions | Scalar value |
df.set_index("Name") |
Set Name as the index | DataFrame |
df.reset_index() |
Restore the default integer index | DataFrame |
7. Practice Exercises
Exercise 1: Select Columns
- Display only the
Namecolumn. - Display the
NameandCitycolumns. - Display the
AgeandMarkscolumns.
Exercise 2: Select Rows and Cells
- Select the first row using
iloc[]. - Select the row at position 2.
- Retrieve Samadhan's marks using the default index.
- Retrieve Pradnya's city using row and column labels.
Hints:
df.iloc[0] df.iloc[2] df.loc[0, "Marks"] df.loc[1, "City"]
Exercise 3: Manage the Index
- Set
Nameas the index. - Select the row labelled
Samadhan. - Reset the index and inspect the result.
indexed_df = df.set_index("Name")
print(indexed_df.loc["Samadhan"])
restored_df = indexed_df.reset_index()
print(restored_df)
8. Revision Questions
- What is the difference between a Series and a DataFrame?
- How do you select two columns from a DataFrame?
- What is the difference between
loc[]andiloc[]? - Why does
iloc[0]select the first row? - What does
set_index()do? - When would you use
reset_index()?
Summary
Selection and indexing help you retrieve the required data from a Pandas DataFrame. Column names are used to select columns, loc[] accesses data by labels, and iloc[] accesses data by integer positions.
Index-management methods help organize rows using meaningful labels. Practise these methods before moving on to more advanced DataFrame operations.