Reputation: 21
A B C D E
0 1.0 2013-01-02 1.0 1 test
1 1.0 2014-01-02 1.0 2 car
2 1.0 2015-01-02 1.0 3 tested
3 1.0 2016-01-02 1.0 4 train
How do I slice a pandas dataframe to contain three consecutive rows out of the above based on the values in column E, e.g. from 'test' to 'tested'?
Upvotes: 2
Views: 4391
Reputation: 323226
I do not why I come up this solution , ugly but work ..
df.iloc[df.index[(df.E=='test').eq(1)].values[0]:df.index[(df.E=='tested').eq(1)].values[0]+1,:]
Out[151]:
A B C D E
0 1.0 2013-01-02 1.0 1 test
1 1.0 2014-01-02 1.0 2 car
2 1.0 2015-01-02 1.0 3 tested
Upvotes: 1
Reputation: 294218
IIUC, use pd.DataFrame.iloc
:
df.iloc[0:3]
A B C D E
0 1.0 2013-01-02 1.0 1 test
1 1.0 2014-01-02 1.0 2 car
2 1.0 2015-01-02 1.0 3 tested
Upvotes: 2