8

I have many pandas dataframe with dates and stock prices like this:

2017-01-04 00:00:00+00:00    103.24

2017-01-05 00:00:00+00:00    103.89

2017-01-06 00:00:00+00:00    102.42

2017-01-09 00:00:00+00:00    102.60

... etc.

What's the best way to filter those pandas by row position?

I was trying something like this, but didn't work.

filter_list = [0, 2]

stock_prices.filter(filter_list)

Thanks.

sacuL
  • 49,704
  • 8
  • 81
  • 106
Contrapunto
  • 172
  • 1
  • 1
  • 10

1 Answers1

12

Use pandas.DataFrame.iloc for filtering by position.

For example, to extract rows 0 and 2:

res = stock_prices.iloc[[0, 2], :]

The first indexing parameter filters by row, the second by column.

The pandas documentation describes in detail various ways of indexing and selecting data.

jpp
  • 159,742
  • 34
  • 281
  • 339