EDIT:
If the number of occurrences of Keyword1 and Keyword2 does not match, then the solutions provided does not work. I have updated the dataframe and code below to reflect a similar mismatch.
ORIGINAL POST:
I have a dataframe of strings and I am trying to choose all the rows between two specific string values [Keyword1 and Keyword2].
I am using the code below:
import pandas as pd
import numpy as np
df=pd.DataFrame(['A', 'B', 'C1', 'D', 'A', 'B', 'C2','C3', 'D','C4', 'B', 'C5'])
df.columns = ['Col1']
Keyword1= 'B'
Keyword2= 'D'
#Filter and delete file mode deliveries
a=df.index[df['Col1'] == Keyword1].tolist()
b=df.index[df['Col1'] == Keyword2].tolist()
b=np.add(b, 1).tolist()
index=[]
for i in range(len(b)):
index_temp=np.arange(a[i],b[i]).tolist()
index=index+index_temp
df_keep= df[df.index.isin(index)]
df_del= df[~df.index.isin(index)]
While this does the work, I am wondering if there a more efficient way to perform the same task.