Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a Pandas dataframe by position of row? [duplicate]

Tags:

python

pandas

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.

like image 461
Contrapunto Avatar asked Apr 16 '18 15:04

Contrapunto


1 Answers

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.

like image 54
jpp Avatar answered Sep 28 '22 06:09

jpp