- One column:
df["name"](a Series); several:df[["name", "city"]] df.loc[row_label, column]: select by labels.df.iloc[row_pos, col_pos]: select by positions.
Pandas Selecting Data
Columns, rows, loc and iloc.
✍️ 3 exercises + code challenge in this lessonKey Takeaways: Pandas Selecting Data
Mastering Pandas Selecting Data helps you write clean and reliable Pandas code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Pandas Selecting Data Code Example
Pandasimport pandas as pd
df = pd.DataFrame({
"name": ["Ali", "Sara", "Usman"],
"city": ["Lahore", "Karachi", "Islamabad"],
"marks": [81, 94, 67],
})
print(df[["name", "marks"]])
print(df.loc[1, "city"])
print(df.iloc[2, 0])
Output
name marks 0 Ali 81 1 Sara 94 2 Usman 67 Karachi Usman
Free sign-in required · Google login available
Pandas Selecting Data Practice Exercises
Check what you learned in “Pandas Selecting Data”.
/ 3 solved
Fill in the blank
Select by row label and column name.
df.[1, "name"]
✓ Correct!
Not quite. Try again.
Multiple choice
Which accessor selects by integer position?
✓ Correct!
Not quite. Try again.
Code challenge
Print the name in the third row (position 2) using iloc.
Write the code in the live editor. Your answer is checked automatically.
✓ Solved
Free sign-in required · Google login available
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services