Get a specific row in a given Pandas DataFrame - GeeksforGeeks (2024)

Improve

Improve

Improve

Like Article

Like

Save Article

Save

Report issue

Report

In the Pandas DataFrame, we can find the specified row value with the function iloc(). In this function, we pass the row number as a parameter. In this article, we will see how we can get a specific row in a given Pandas Dataframe.

pandas.DataFrame.iloc[] Function Syntax

Syntax : pandas.DataFrame.iloc[]

Parameters :

  • Index Position : Index position of rows in integer or list of integer
  • Return type : Data frame or Series depending on parameters

Print a Specific Row of a Pandas DataFrame Examples

Example 1: Specific row in a given Pandas DataFrame

In this example, we are using iloc function. The code creates a DataFrame from a dictionary, prints the original DataFrame, and then displays the values of the second row using the iloc method.

python3

# importing the module

import pandas as pd

# creating a DataFrame

data = {'1' : ['g', 'e', 'e'],

'2' : ['k', 's', 'f'],

'3' : ['o', 'r', 'g'],

'4' : ['e', 'e', 'k']}

df = pd.DataFrame(data)

print("Original DataFrame")

display(df)

print("Value of row 1")

display(df.iloc[1])

 
 

Output

Original DataFrame
1 2 3 4
0 g k o e
1 e s r e
2 e f g k
Value of row 1
1 e
2 s
3 r
4 e
Name: 1, dtype: object

Example 2: Get row in a given Pandas DataFrame

The code generates a DataFrame containing student names and their scores in three subjects. It then prints the original DataFrame and displays the values of the fourth row (index 3) corresponding to the student ‘Alex’ using the iloc method.

python3

# importing the module

import pandas as pd

# creating a DataFrame

data = {'Name' : ['Simon', 'Marsh', 'Gaurav',

'Alex', 'Selena'],

'Maths' : [8, 5, 6, 9, 7],

'Science' : [7, 9, 5, 4, 7],

'English' : [7, 4, 7, 6, 8]}

df = pd.DataFrame(data)

print("Original DataFrame")

display(df)

print("Value of row 3 (Alex)")

display(df.iloc[3])

 
 

Output

Original DataFrame
Name Maths Science English
0 Simon 8 7 7
1 Marsh 5 9 4
2 Gaurav 6 5 7
3 Alex 9 4 6
4 Selena 7 7 8
Value of row 3 (Alex)
Name Alex
Maths 9
Science 4
English 6
Name: 3, dtype: object


Last Updated : 30 Nov, 2023

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Get a specific row in a given Pandas DataFrame - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 6052

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.