46

Suppose a Pandas dataframe looks like:

    BoxRatio  Thrust  Velocity  OnBalRun  vwapGain
5     -0.163  -0.817     0.741     1.702     0.218
8      0.000   0.000     0.732     1.798     0.307
11     0.417  -0.298     2.036     4.107     1.793
13     0.054  -0.574     1.323     2.553     1.185

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:

 0.417  -0.298     2.036     4.107     1.793
cottontail
  • 10,268
  • 18
  • 50
  • 51
CBrauer
  • 1,035
  • 2
  • 11
  • 17
  • 1
    Have you https://stackoverflow.com/questions/16096627/pandas-select-row-of-data-frame-by-integer-index? – Zero Sep 19 '17 at 18:32
  • actually Zero has found detailed appropriate answer – bellum Sep 19 '17 at 18:50
  • Possible duplicate of [Pandas select row of data frame by integer index](https://stackoverflow.com/questions/16096627/pandas-select-row-of-data-frame-by-integer-index) – bellum Sep 19 '17 at 18:54

3 Answers3

79

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
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
3

Alternatively you can also use take:

In [4]: df.take([2])
Out[4]: 
    BoxRatio  Thrust  Velocity  OnBalRun  vwapGain
11     0.417  -0.298     2.036     4.107     1.793
rachwa
  • 1,805
  • 1
  • 14
  • 17
0

You can also slice the dataframe. For example, to get the third row as a dataframe, use slice 2:3.

row3 = df.iloc[2:3]
cottontail
  • 10,268
  • 18
  • 50
  • 51