Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe: How to select a row by index, and then get the next few rows

In pandas, I have a Dataframe indexed by timestamp. Looks like the following table:

                 A      B     C     D  
DATE                                                             
2018-01-17        157.52        163.74       157.28         159.84   
2018-01-16        158.25        159.35       155.93         157.40   
2018-01-15        157.15        159.59       156.79         158.86   
2018-01-12        158.25        158.62       157.40         157.52  

And I'm trying to select a row by index, and also select the next few rows. (For example, select two rows start at 2018-01-12). I found both .loc and .iloc are hard to do such task. Is there any other ways to do that?

like image 219
Toby Mao Avatar asked Feb 21 '18 17:02

Toby Mao


People also ask

How do you grab rows in Pandas?

To get the nth row in a Pandas DataFrame, we can use the iloc() method. For example, df. iloc[4] will return the 5th row because row numbers start from 0.


3 Answers

Solution #1: Using the DataFrame's index, followed by head(2):

df['2018-01-12':].head(2)

Solution #2: Using iloc:

i = df.index.get_loc('2018-01-12')
df.iloc[i:i+2]

Bonus solution #3: It seems like you're analyzing stock data. Maybe you're interested in something that could be more efficiently done using rolling windows? (Moving Averages maybe?) Consider using rolling, e.g. to calculate the rolling mean:

df.rolling(2).mean()
like image 102
Yaniv Avatar answered Oct 22 '22 10:10

Yaniv


You should be able to do something like:

date_of_wanted_row = "2018-01-12"
iloc_of_wanted_row = df.index.get_loc(date_of_wanted_row)
return df.iloc[iloc_of_wanted_row: iloc_of_wanted_row + 4]

(but with more sensible variable names, and I'm assuming the dates in the index are not really strings)

like image 38
LangeHaare Avatar answered Oct 22 '22 09:10

LangeHaare


You can use (to get values between dates):

df.loc['2018-01-12':'2018-01-14']

or, to get all data from January:

df.loc['2018-01']
like image 29
vstram Avatar answered Oct 22 '22 09:10

vstram