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, eg from 'test' to 'tested'?
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.