Skip to content
Advertisement

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

Suppose a Pandas dataframe looks like:

JavaScript

How can I extract the third row (as row3) as a pandas dataframe? In other words, row3.shape should be (1,5) and row3.head() should be:

JavaScript

Advertisement

Answer

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

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement