I need to extract rows if certain conditions are applied.
- the column
col1should contain all the words in the listlist_words. - the last word should be
Story - the last word in the next row should b
ac:
This is my current code:
import pandas as pd
df = pd.DataFrame({'col1': ['Draft SW Quality Assurance Story', 'alex ac', 'anny ac', 'antoine ac','aze epic', 'bella ac', 'Complete SW Quality Assurance Plan Story', 'celine ac','wqas epic', 'karmen ac', 'kameilia ac', 'Update SW Quality Assurance Plan Story', 'joseph ac','Update SW Quality Assurance Plan ac', 'joseph ac'],
'col2': ['aa', 'bb', 'cc', 'dd','ee', 'ff', 'gg', 'hh', 'ii', 'jj', 'kk', 'll', 'mm', 'nn', 'oo']})
print(df)
list_words="SW Quality Plan Story"
set_words = set(list_words.split())
#check if list_words is in the cell
df['TrueFalse']=pd.concat([df.col1.str.contains(word,regex=False) for word in list_words.split()],axis=1).sum(1) > 1
print('\n',df)
#extract last word
df["Suffix"] = df["col1"].str.split().str[-1]
print('\n',df)
df['ok']=''
for i in range (len(df)-1):
if ((df["Suffix"].iloc[i]=='Story') & (df["TrueFalse"].iloc[i]=='True') & (df["Suffix"].iloc[i+1]=='ac')):
df['ok'].iloc[i+1]=df["Suffix"].iloc[i+1]
print('\n',df)
output:
col1 col2 TrueFalse Suffix ok
0 Draft SW Quality Assurance Story aa True Story
1 alex ac bb False ac
2 anny ac cc False ac
3 antoine ac dd False ac
4 aze epic ee False epic
5 bella ac ff False ac
6 Complete SW Quality Assurance Plan Story gg True Story
7 celine ac hh False ac
8 wqas epic ii False epic
9 karmen ac jj False ac
10 kameilia ac kk False ac
11 Update SW Quality Assurance Plan Story ll True Story
12 joseph ac mm False ac
13 Update SW Quality Assurance Plan ac nn True ac
14 joseph ac oo False ac
line 13 should be set to False
desired output :
col1 col2 TrueFalse Suffix
1 Complete SW Quality Assurance Plan Story gg True Story
2 celine ac hh True ac
3 Update SW Quality Assurance Plan Story ll True Story
4 joseph ac mm True ac