Selecting Columns and Rows
When analyzing data, you often need to focus on specific parts, such as selecting a column of interest or checking a few rows.
In Pandas, this can be done using column labels
, row positions
, and index labels
.
Selecting Columns
The most reliable way to select a column is by using square brackets with the column name:
Select a column by 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 select rows, use either:
iloc[]
for position-based accessloc[]
for label-based access
Select the first row by position
df.iloc[0]
Select the row with label 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:
Select a single 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"] |
Quiz
0 / 1
You can use df.ColumnName
(dot notation) to reliably select columns in a Pandas DataFrame.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help