Python 如何在两个列值之间按行分割数据帧?
如何根据列E中的值(例如从“测试”到“测试”)对pandas数据帧进行切片以包含上述三行中的连续行?IIUC,使用Python 如何在两个列值之间按行分割数据帧?,python,pandas,dataframe,filter,slice,Python,Pandas,Dataframe,Filter,Slice,如何根据列E中的值(例如从“测试”到“测试”)对pandas数据帧进行切片以包含上述三行中的连续行?IIUC,使用pd.dataframe.iloc: 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
pd.dataframe.iloc
:
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
我不知道为什么我会提出这个解决方案,但丑陋的工作
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