MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Indexing with loc & iloc0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
3.1Indexing with loc & iloc3.2Boolean Indexing & DataFrame Querying3.3Setting, Resetting & Managing Indices
HomeTutorialsPandasIndexing with loc & iloc
Boolean Indexing & DataFrame Querying

Indexing with loc & iloc

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The Core Indexers in Pandas

Accessing subsets of data in Pandas is achieved primarily through two explicit indexing accessors:

  1. 1
    loc: Label-based indexing (select by column names and row index labels).
  2. 2
    iloc: Integer position-based indexing (select by integer offset: 0, 1, 2, ...).
Golden Rule: loc uses names/labels, iloc uses integer positions.

2. Setting Up Sample Data

1import pandas as pd
2 
3data = {
4 'Name': ['Aarav', 'Priya', 'Rohan', 'Sneha', 'Kabir'],
5 'City': ['Delhi', 'Mumbai', 'Bengaluru', 'Noida', 'Agra'],
6 'Score': [92, 88, 95, 79, 85]
7}
8# Using custom index labels
9df = pd.DataFrame(data, index=['R101', 'R102', 'R103', 'R104', 'R105'])
10print(df)

3. Label-Based Indexing with .loc[row_label, col_label]

A. Selecting Single Cell or Single Row

1# Get row 'R101' as a Series
2row_101 = df.loc['R101']
3 
4# Get specific cell: Row 'R102', Column 'Score'
5score_102 = df.loc['R102', 'Score'] # Returns 88

B. Slicing with .loc (INCLUSIVE of endpoints!)

Crucial Distinction: In standard Python slicing [0:3], the stop endpoint 3 is excluded. But with loc, both the start label and the stop label are INCLUDED!
1# Rows from R102 through R104 inclusive, and columns 'Name' through 'Score'
2subset = df.loc['R102':'R104', 'Name':'Score']
3print(subset)

4. Integer-Based Indexing with .iloc[row_idx, col_idx]

.iloc operates strictly on 0-based integer positions, adhering to standard Python slice rules (stop position is EXCLUDED):

1# Get the first row (index 0)
2first_row = df.iloc[0]
3 
4# Get the last row using negative indexing
5last_row = df.iloc[-1]
6 
7# Slice first 3 rows (0, 1, 2) and first 2 columns (0, 1)
8sub_matrix = df.iloc[0:3, 0:2]
9print(sub_matrix)

5. Summary Cheat Table

Featuredf.loc[]df.iloc[]
Lookup ModeBy Label / NameBy Integer Position
Row Selectiondf.loc['R101']df.iloc[0]
Slice EndpointsInclusive ('A':'C' includes C)Exclusive (0:3 stops at 2)
Boolean MaskingSupported (df.loc[df['Score'] > 90])Not directly supported

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What does the 'i' in .iloc stand for?
Question 2
How does slicing endpoint behavior differ between .loc and .iloc?
Question 3
Which command selects the very last row of any DataFrame regardless of its index labels?
Question 4
Given a DataFrame with index ['A', 'B', 'C'] and column 'Revenue', how do you access the Revenue of row 'B' using label-based indexing?
Question 5
What error occurs if you pass a string column name like 'Score' into .iloc?

Next Lesson

Boolean Indexing & DataFrame Querying

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Reading JSON, SQL Databases & Parquet FilesBoolean Indexing & DataFrame Querying

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext