Saturday, November 16, 2024
Google search engine
HomeLanguagesGet a specific row in a given Pandas DataFrame

Get a specific row in a given Pandas DataFrame

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.

pandas.DataFrame.iloc[]

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

Example 1: Specific row in a given Pandas DataFrame

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

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

RELATED ARTICLES

Most Popular

Recent Comments