Pandas DataFrame iloc

Integer based position location using iloc
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]}
my_data = pd.DataFrame(data=my_dict)

print(my_data.iloc[0]) # First rows 
Output
NAME       Ravi
ID            1
MATH         30
ENGLISH      20
Name: 0, dtype: object

Second row

print(my_data.iloc[1])
Output
NAME       Raju
ID            2
MATH         40
ENGLISH      30
Name: 1, dtype: object

Value at position

print(my_data.iloc[1,2]) # 40 
First two rows
print(my_data.iloc[:2])
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    30       20
1  Raju   2    40       30
print(my_data.iloc[[0]]) 
output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    30       20

Last record

print(my_data.iloc[-1])
NAME       Jack
ID            6
MATH         80
ENGLISH      70
Name: 5, dtype: object
A column (MATH) value of last record.
print(my_data.iloc[-1,2]) # 80

Range of rows

Record from 2 to 5 ( excluding 5 )
print(my_data.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(my_data.iloc[[0,-1]]) # first and last records
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    30       20
5  Jack   6    80       70

Records with matching conditions

Students with more than or equal to 50 in MATH
print(my_data[my_data['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
Pandas DataFrame loc - rows and columns by column label


plus2net.com



Post your comments , suggestion , error , requirements etc here




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