close

How can I extract the nth row of a pandas data frame as a pandas data frame?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I extract the nth row of a pandas data frame as a pandas data frame in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How can I extract the nth row of a pandas data frame as a pandas data frame?

  1. How can I extract the nth row of a pandas data frame as a pandas data frame?

    Use .iloc with double brackets to extract a DataFrame, or single brackets to pull out a Series.

  2. extract the nth row of a pandas data frame as a pandas data frame

    Use .iloc with double brackets to extract a DataFrame, or single brackets to pull out a Series.

Method 1

Use .iloc with double brackets to extract a DataFrame, or single brackets to pull out a Series.

>>> import pandas as pd
>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
>>> df
   col1  col2
0     1     3
1     2     4
>>> df.iloc[[1]]  # DataFrame result
   col1  col2
1     2     4
>>> df.iloc[1]  # Series result
col1    2
col2    4
Name: 1, dtype: int64

This extends to other forms of DataFrame indexing as well, namely .loc and .__getitem__():

>>> df.loc[:, ['col2']]
   col2
0     3
1     4

>>> df[['col2']]
   col2
0     3
1     4

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read