简体   繁体   English

如何按两列值之间的行对熊猫数据框进行切片?

[英]How to slice a pandas dataframe by rows between two column values?

     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'?如何根据列 E 中的值(例如,从“test”到“tested”)将 Pandas 数据帧切片以包含上述三个连续行?

IIUC, use pd.DataFrame.iloc : 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python Pandas DataFrame中切片列值 - How to slice column values in Python pandas DataFrame 如何在 Python Pandas 中的两个值之间选择 DataFrame 中的行? - How to select rows in a DataFrame between two values, in Python Pandas? 如何计算 pandas dataframe 的两个值之间的行数? - How to count rows between two values of a pandas dataframe? 如何计算 pandas dataframe 中两个不同值之间的行数? - How to calculate the number of rows between two different values in a pandas dataframe? 如何在熊猫数据框中的第一行和最后一行之间切片行? - How to slice rows between first and last row in pandas dataframe? 如何在列的两个值之间选择数据框中的所有行 - How to select all rows in a dataframe between two values of column 如何在 dataframe 中在一列中的两个值之间迭代行? - How to iterate over rows in a dataframe between two values in one column? Pandas:计算一列中两个值之间的行数 - Pandas: count how many rows between two values in a column 如何在 pandas dataframe 中通过在两行之间划分特定列中的值并保持其他列不变来创建新行? - How to create a new row in pandas dataframe by dividing values in a specific column between two rows and keeping other columns intact? 如何一次读取熊猫数据框的两行两列,并在这些行/列值上应用条件? - How to read two rows and two columns of a pandas dataframe at once and apply conditions on those rows/column values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM