Select a row of series or dataframe by given integer index

Dataframe.iloc[]  is used to select a row of series/dataframe by given integer index. Let’s Create a Data Frame:

Code:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd 
  
# Create the dataframe 
df = pd.DataFrame({'ID': ['114', '345',
                         '157788', '5626'], 
                'Product': ['shirt', 'trousers',
                           'tie', 'belt'], 
                'Price': [1200, 1500,
                         600, 352], 
                'Color': ['White','Black'
                         'Red', 'Brown'], 
                'Discount': [10, 10,
                            10, 10]}) 
  
# Show the dataframe
df

chevron_right


Output:

Dataframe



Now, Selecting a row of series/dataframe by a given integer index:
Example 1: Selecting the first row only.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# select first row 
# from the dataframe
df.iloc[0]

chevron_right


Output: 
 

first row of dataframe

Example 2: Selecting  0,1,2  rows.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# select 0, 1, 2 rows
#from the dataframe
df.iloc[0 : 3]

chevron_right


Output:
 

extract rows of dataframe



Example 3: Selecting rows from 0 to 2 and columns 0 to 1.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# selecting rows from 0 to
# 2 and columns 0 to 1
df.iloc[0 : 3, 0 : 2]

chevron_right


Output: 
 

Dataframe subset

Example 4: Selecting all rows and columns from 0 to 3. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# selecting all rows and 
# columns from 0 to 3
df.iloc[ : , 0 : 4]

chevron_right


Output:
 

Example 5: Selecting all rows and 2nd column.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# selecting all rows and 
# 3rd column
df.iloc[ : , 2]

chevron_right


Output:
 

dataframe with 2nd column only




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.