Pandas iloc to get rows based on integer position using callback functions and array from DataFrame
Difference between iloc and loc
loc is label based so we have to use column names. iloc is position based ( integer ).
Copy the dataframe structure without data
df2 = df.iloc[:0] # create dataframe of same structure or columns
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],'MATH':[30,40,50,60,70,80],
'ENGLISH':[20,30,40,50,60,70]}
df = pd.DataFrame(data=my_dict)
print(df.iloc[0]) # First rows
Output
NAME Ravi
ID 1
MATH 30
ENGLISH 20
Name: 0, dtype: object
Second row
print(df.iloc[1])
Output
NAME Raju
ID 2
MATH 40
ENGLISH 30
Name: 1, dtype: object
Value at position
print(df.iloc[1,2]) # 40
First two rows
print(df.iloc[:2])
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
All records
print(df.iloc[::]) # all rows all columns
print(df.iloc[:,:]) # all rows all columns
print(df.iloc[:,2]) # all rows class column
print(df.iloc[[0]])
output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
Last record
print(df.iloc[-1])
NAME Jack
ID 6
MATH 80
ENGLISH 70
Name: 5, dtype: object
A column (MATH) value of last record.
print(df.iloc[-1,2]) # 80
Range of rows
Record from 2 to 5 ( excluding 5 )
print(df.iloc[2:5]) # range of records
Output
NAME ID MATH ENGLISH
2 Alex 3 50 40
3 Ron 4 60 50
4 King 5 70 60
First and Last record
print(df.iloc[[0,-1]]) # first and last records
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
5 Jack 6 80 70
For better understanding we can use bigger DataFrame with more rows. Copy and use or download the CSV or Excel file to create the sample DataFrame with 35 rows and 5 columns. Student ( Sample ) DataFrame →
Alternate rows of the DataFrame
print(df.iloc[::2]) alternate rows
print(df.iloc[::3]) # All third rows starting from first
iloc with Callable function
print(df.iloc[lambda x: x.index % 2 == 0]) # All even rows based on Index value
print(df.iloc[lambda x: [4,6,12],:]) # records based on index value
print(df.iloc[lambda x: list(x.id % 2 == 0)]) # All even rows based on a column value
print(df.iloc[lambda x: list(x.mark>90),:]) # records based condition
Using boolean array
We must match the number of array element with number of rows or columns of the DataFrame.
print(df.iloc[:,[True,False,True,False,True]]) # show all rows with matching columns
To match the number of rows , we have created one new DataFrame with 3 number of row and then used the array.
df2=df.iloc[0:3] # New DataFrame with 3 rows
print(df2.iloc[[True,False,True]]) # Matching rows based on Index value
Summary of using iloc
print(df.iloc[::]) # all rows
print(df.iloc[0]) # first record
print(df.iloc[1]) # second record
print(df.iloc[1,3]) # second record 3rd element ( 85 )
print(df.iloc[:,1]) # All rows name column
print(df.iloc[-1]) # show last record
print(df.iloc[[-1]]) # show last record in full
print(df.iloc[:2]) # first two records
print(df.iloc[:-2]) # All records except last two
print(df.iloc[-2]) # 2nd from last record
print(df.iloc[-2:]) # Last two records
print(df.iloc[2:5]) # range of records, 3rd to 5th
print(df.iloc[[0,-1]]) # first and last row
print(df.iloc[::2]) # alternate rows from DataFrame
print(df.iloc[::3]) # All third rows starting from first
Records with matching conditions
Students with more than or equal to 50 in MATH
print(df[df['MATH']>=50])
Output is here
NAME ID MATH ENGLISH
2 Alex 3 50 40
3 Ron 4 60 50
4 King 5 70 60
5 Jack 6 80 70
Update record based on iloc
Update mark for the row with index 33.
df.iloc[33, df.columns.get_loc('mark')] = 100
Update mark for all students to 100
df.iloc[:, df.columns.get_loc('mark')] = 100
Update mark for all students who got equal to or above 90