Lesson 08
Selecting Columns and Rows
Pull one column or several out of a DataFrame with square brackets, slice rows by position, and learn why chained selection is a poor place to assign.
Square brackets on a DataFrame do two different jobs. Give them a column name and you get that column. Give them a slice and you get rows. Knowing which one you asked for is most of the skill.
One column
A single name returns a Series. The same name inside a list returns a one-column DataFrame instead. The values are the same; the shape is not, and later code cares about the difference.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey"],
"hours": [42, 31, 55],
})
print(learners["name"])
print(type(learners["name"]))
print(learners[["name"]])
print(type(learners[["name"]]))Output:
0 Avery
1 Jordan
2 Casey
Name: name, dtype: object
<class 'pandas.core.series.Series'>
name
0 Avery
1 Jordan
2 Casey
<class 'pandas.core.frame.DataFrame'>Several columns
Pass a list of names to keep several columns. The list also sets the order, so this is how you rearrange a table for a report. Asking for a name that does not exist raises a KeyError.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
print(learners[["name", "score", "hours"]])
print(learners.columns.tolist())Output:
name score hours
0 Avery 88 42
1 Jordan 74 31
2 Casey 91 55
3 Riley 65 24
4 Morgan 82 47
5 Quinn 79 38
['name', 'track', 'city', 'hours', 'score']Rows by position
A slice inside the brackets selects rows, not columns. It counts positions, and like every Python slice it stops before the second number. A step works too.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
print(learners[0:3])
print(learners[::2])
print(learners[-2:])Output:
name track city hours score
0 Avery Python Northgate 42 88
1 Jordan NumPy Eastport 31 74
2 Casey Python Northgate 55 91
name track city hours score
0 Avery Python Northgate 42 88
2 Casey Python Northgate 55 91
4 Morgan NumPy Eastport 47 82
name track city hours score
4 Morgan NumPy Eastport 47 82
5 Quinn pandas Westfield 38 79Columns and rows together
Select the columns, then slice the rows of the result. Each step is plain and easy to read, which matters more than saving a line.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
picked = learners[["name", "track", "score"]]
print(picked[1:4])
print(learners[2:5][["name", "hours"]])Output:
name track score
1 Jordan NumPy 74
2 Casey Python 91
3 Riley pandas 65
name hours
2 Casey 55
3 Riley 24
4 Morgan 47Do not assign through two brackets
Reading through two selections is fine. Writing through them is not: the first selection may hand back a copy, so your change lands on the copy and the original keeps its old value. Use .loc for any write, which the next lesson covers.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey"],
"score": [88, 74, 91],
})
learners.loc[learners["name"] == "Jordan", "score"] = 80
print(learners)Output:
name score
0 Avery 88
1 Jordan 80
2 Casey 91One rule keeps you safe: read with brackets, write with .loc or .iloc.
Key takeaways
-
df["col"] gives a Series and df[["col"]] gives a DataFrame
-
A list of names picks columns and sets their order
-
A slice in the brackets picks rows by position and stops before the end
-
Write with .loc, never through two sets of brackets
-
Next: loc and iloc
-
Previous: DataFrame Attributes
Loading playground…
Check your understanding
Selecting Columns and Rows Quiz
What type is stock["price"]?
1 / 10