如何在两列值之间按行分割熊猫数据框?

如何在两列值之间按行分割熊猫数据框?

问题描述:

 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 

如何根据列E中的值例如分割熊猫数据框以包含上面的连续三行,从'测试'到'测试'?如何在两列值之间按行分割熊猫数据框?

+1

你认为答案是...告诉我们什么你的预期结果看起来像。 – piRSquared

IIUC,使用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 

我为什么我不上来这个解决方案,丑陋但工作..

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