CodeMIRAI
0 XP

Selecting rows and columns

2 min read 6 runnable examples Quiz included

Every example uses this small table:

Python
import pandas as pd

df = pd.DataFrame({
    "name": ["Asha", "Leo", "Mei", "Raj", "Zoe"],
    "course": ["Python", "SQL", "Python", "Java", "SQL"],
    "score": [92, 78, 85, 88, 95],
    "city": ["Mumbai", "London", "Osaka", "Delhi", "Rome"],
})
print(df["name"])                 # one column: a Series
print(df[["name", "score"]])      # a list of columns: a DataFrame

Note the double brackets in df[["name", "score"]]: the outer pair selects, and the inner pair is the list of columns.

Rows by position: iloc

Python
import pandas as pd

df = pd.DataFrame({
    "name": ["Asha", "Leo", "Mei", "Raj", "Zoe"],
    "course": ["Python", "SQL", "Python", "Java", "SQL"],
    "score": [92, 78, 85, 88, 95],
})
print(df.iloc[0])             # the first row, as a Series
print(df.iloc[1:3])           # rows 1 and 2
print(df.iloc[-1]["name"])    # the last row's name
print(df.iloc[0, 2])          # row 0, column 2
print(df.iloc[:3, [0, 2]])    # the first three rows, columns 0 and 2

iloc takes positions, as df.iloc[rows, columns], and slices exclude their end, like Python's.

Rows by label: loc

Python
import pandas as pd

df = pd.DataFrame({
    "name": ["Asha", "Leo", "Mei", "Raj", "Zoe"],
    "course": ["Python", "SQL", "Python", "Java", "SQL"],
    "score": [92, 78, 85, 88, 95],
})
students = df.set_index("name")           # names become the row labels
print(students.loc["Mei"])
print(students.loc["Leo":"Raj"])          # label slices include the end
print(students.loc[["Zoe", "Asha"], ["course", "score"]])
print(students.loc["Raj", "score"])

set_index turns a column into row labels, so rows can be fetched by something meaningful rather than a number. loc takes labels, as df.loc[rows, columns].

Conditions and columns together

Python
import pandas as pd

df = pd.DataFrame({
    "name": ["Asha", "Leo", "Mei", "Raj", "Zoe"],
    "course": ["Python", "SQL", "Python", "Java", "SQL"],
    "score": [92, 78, 85, 88, 95],
})
print(df.loc[df["score"] > 85, ["name", "score"]])
print(df.loc[(df["course"] == "SQL") & (df["score"] > 80), "name"])

loc accepts a condition for the rows and a column choice in one step.

Single values: at and iat

Python
import pandas as pd

df = pd.DataFrame({
    "name": ["Asha", "Leo", "Mei"],
    "course": ["Python", "SQL", "Python"],
    "city": ["Mumbai", "London", "Osaka"],
})
print(df.set_index("name").at["Leo", "city"])    # by labels
print(df.iat[2, 1])                              # by positions: row 2, column 1

at and iat fetch exactly one value, faster than loc and iloc do.

Changing values

Python
import pandas as pd

df = pd.DataFrame({
    "name": ["Asha", "Leo", "Mei", "Raj", "Zoe"],
    "course": ["Python", "SQL", "Python", "Java", "SQL"],
    "score": [92, 78, 85, 88, 95],
})
df.loc[df["name"] == "Leo", "score"] = 81       # one cell, chosen by a condition
df.loc[df["course"] == "SQL", "score"] += 2     # several cells at once
print(df)
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.