Pandas DataFrame iloc

Integer based position location using iloc

Pandas iloc to get rows based on integer position using callback functions and array from DataFrame


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

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
df.iloc[df['mark']>=90, df.columns.get_loc('mark')] = 100
Replace the matching string. Here name column is replaced with abcd where it is equal to Kenn Rein.
df.iloc[df['name']=='Kenn Rein',df.columns.get_loc('name')]='abcd'

Updating using for loop

for i in df.index:
    df.iloc[i,3]=51 # update the 4th column with 51
Pandas Pandas DataFrame loc - rows and columns by column label
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer