从大型excel文件熊猫中提取两个特定单元格之间的行

时间:2020-04-20 09:50:06

标签: python-3.x pandas extract rows

我需要从Issue Type列中提取单元史诗和故事之间的n行。 当我研究一个简单的示例时,this code的工作非常完美:

import pandas as pd
df = pd.DataFrame({'col1': ['aze ', 'az a', 'az a', 'azs abv','aze ', 'az a', 'azs abv', 'abc 45','wqas', 'foo bar abc', 'foo abv', 'abc 45', 'abc 45'], 'col2': ['epic', 'ac4', 'ac5', 'story','epic', 'story', 'ac6', 'ac7','epic', 'ac1', 'ac2', 'ac3', 'story'], 'col3': ['hey', 'hello', 'hola', 'yoopy','hawdi', 'yiiha', 'yow', 'yalla', 'yiiha', 'yow', 'yalla', 'yalla', 'yalla']}) 
print(df)
Epic = df['col2'].eq('Epic') 
Story = df['col2'].eq('Story')
df1=df.loc[(Epic | Story).where(Epic.cumsum().ge(1), False)
                     .cumsum()
                     .mod(2)
                     .eq(1)
                     .where(~Epic, False)].reset_index(drop=True)
print('\n',df1)
df1.to_excel('EpicStory.xlsx', 'Sheet1', index=True) 

但是当我使用excel文件作为输入时,输出不正确 这是我的输入:

enter image description here

这是输出: enter image description here

这是所需的输出:

enter image description here

但是我不能把头缠住!

0 个答案:

没有答案