Selecting Columns and Rows
When analyzing data, you often need to focus on specific subsets — like selecting one column of interest or reviewing certain rows.
In Pandas, you can do this easily using column labels, row positions, or index labels.
Selecting Columns
The most common and reliable way to select a column is by using square brackets with its column name:
df["Population"]
This returns a Series. You can assign it to a variable, combine it with conditions, or perform calculations.
Avoid using
df.ColumnName(dot notation) because it only works when column names are valid Python identifiers.
Selecting Rows
To access rows, use one of two methods:
iloc[]— selects rows by positionloc[]— selects rows by label
df.iloc[0]
df.loc[0]
Both return a row as a Series.
Selecting a Specific Value
Combine row and column selection to get a single cell value:
df.loc[0, "Population"]
This retrieves the value at row 0 in the "Population" column.
Summary
| Selector | Access Type | Example |
|---|---|---|
df["col"] | Column by name | df["Age"] |
df.iloc[i] | Row by position | df.iloc[3] |
df.loc[i] | Row by label | df.loc[3] |
df.loc[i, "col"] | Cell | df.loc[3, "Age"] |
Lessons in this chapter · Taming Data with Pandas
- 1. Introduction to Pandas and DataFrames
- 2. Creating and Inspecting Series and DataFrames
- 3. Selecting Columns and Rows
- 4. Filtering with Boolean Conditions
- 5. Updating and Modifying Data
- 6. Anatomy of a DataFrame
- 7. Multiple-choice quiz
- 8. Handling Missing and Duplicate Data
- 9. Sorting, Ranking, and Reindexing
- 10. GroupBy and Aggregation Functions
- 11. Merging and Joining DataFrames
- 12. Descriptive Statistics and Value Counts
- 13. Working with Date and Time Columns
- 14. Fill-in-the-blank quiz
You can use df.ColumnName (dot notation) to reliably select columns in a Pandas DataFrame.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help