Informatics Practices · Class 12 · Python Pandas · DataFrame
PandasDataFrame⏱️ 11 min read
Rows & Subsets: loc / iloc
Selecting columns was easy. To pull out rows — or a rectangle of rows and columns — you use .loc and .iloc, exactly like a Series but now with two directions.
1Grab a rectangle
The syntax is df.loc[rows, columns]. Drag the pickers to select a subset and watch the golden rule in action: .loc keeps the end, .iloc drops it.
🎯 loc / iloc subset explorer
Grab a rectangle of the DataFrame by rows AND columns. .loc uses labels and INCLUDES the end; .iloc uses positions and EXCLUDES it.
df.loc['ID 1':'ID 4', 'Name':'IP']
Name
English
IP
Maths
ID 1
Rinku
65
89
72
ID 2
Ritu
77
83
69
ID 3
Pankaj
72
95
87
ID 4
Ajay
81
99
94
ID 5
Aditya
92
87
96
Selected 4 rows × 3 columns.
End labels are INCLUDED ✓
2.loc — by label, inclusive
.loc uses the row and column labels, and both the start and end labels are included. Use :on either side to mean "all".
You can also select rows and columns discretely (specific, non-adjacent items) by passing a list of labels instead of a slice:
df_loc.py
import pandas as pd
df = pd.DataFrame({'Name':['Rinku','Ritu','Pankaj','Ajay','Aditya'],'English Marks':[65,77,72,81,92],'IP Marks':[89,83,95,99,87],'Maths Marks':[72,69,87,94,96],}, index=['ID 1','ID 2','ID 3','ID 4','ID 5'])# rows ID 1 to ID 3, columns Name to IP Marks (all inclusive)print(df.loc['ID 1':'ID 3','Name':'IP Marks'])# Discrete selection: specific rows and columns using listsprint("\n--- Discrete Selection (lists of labels) ---")print(df.loc[['ID 1','ID 3','ID 5'],['Name','IP Marks']])# a single row (all its columns)print("\n--- df.loc['ID 2'] ---")print(df.loc['ID 2'])
3.iloc — by position, exclusive
.iloc uses integer positions (starting at 0), and the end position is excluded — just like list slicing.
Just like with .loc, you can select specific rows and columnsdiscretely by passing a list of integer positions:
df_iloc.py
import pandas as pd
df = pd.DataFrame({'Name':['Rinku','Ritu','Pankaj','Ajay','Aditya'],'English Marks':[65,77,72,81,92],'IP Marks':[89,83,95,99,87],'Maths Marks':[72,69,87,94,96],}, index=['ID 1','ID 2','ID 3','ID 4','ID 5'])# rows 0,1 and columns 0,1,2 (end excluded)print(df.iloc[0:2,0:3])# Discrete selection: specific row/column index positions using listsprint("\n--- Discrete Selection (lists of positions) ---")print(df.iloc[[0,2,4],[0,2]])# every row, first two columnsprint("\n--- df.iloc[:, 0:2] ---")print(df.iloc[:,0:2])
4.loc with conditions
A superpower of .loc (that .iloc doesn't have): you can pass a condition for the rows and even list the columns you want in the same call.
df_loc_cond.py
import pandas as pd
df = pd.DataFrame({'Name':['Rinku','Ritu','Pankaj','Ajay','Aditya'],'English Marks':[65,77,72,81,92],'IP Marks':[89,83,95,99,87],'Maths Marks':[72,69,87,94,96],}, index=['ID 1','ID 2','ID 3','ID 4','ID 5'])# rows where IP Marks > 88, only Name and IP Marks columnsprint(df.loc[df['IP Marks']>88,['Name','IP Marks']])
5A single value: at & iat
To read one cell, the fastest tools are .at (by labels) and .iat (by positions) — the DataFrame twins of the Series .at/.iat.
df_at_iat.py
import pandas as pd
df = pd.DataFrame({'Name':['Rinku','Ritu','Pankaj','Ajay','Aditya'],'English Marks':[65,77,72,81,92],'IP Marks':[89,83,95,99,87],'Maths Marks':[72,69,87,94,96],}, index=['ID 1','ID 2','ID 3','ID 4','ID 5'])print("df.at['ID 4', 'Maths Marks'] :", df.at['ID 4','Maths Marks'])print("df.iat[3, 3] :", df.iat[3,3])# the [] shortcut is column-first, then rowprint("df['Maths Marks']['ID 4'] :", df['Maths Marks']['ID 4'])
🔑Key Takeaway
.loc = labels, end included. .iloc = positions, end excluded. Both take [rows, columns]. For a single cell use .at (labels) or .iat (positions).
✏️ Quick Check
df has rows ID 1–ID 5. What does df.loc['ID 2':'ID 4'] return?