This is a follow up to this stackoverflow questions
Pandas: How to return rows where a column has a line breaks/new line ( \n ) in its cell?
Which shows how to get a word which follows a new line.
I would now like to return rows where the column can have one of of several case-sensitive words which follows right after a new line.
Here is a minimal example
testdf = pd.DataFrame([
[ ' generates the final summary. \nRESULTS We evaluate the performance of ', ],
[ 'the cat and bat \n\n\nRESULTS\n teamed up to find some food'],
['anthropology with RESULTS pharmacology and biology'],
[ ' generates the final summary. \nMethods We evaluate the performance of ', ],
[ 'the cat and bat \n\n\nMETHODS\n teamed up to find some food'],
['anthropology with METHODS pharmacology and biology'],
[ ' generates the final summary. \nBACKGROUND We evaluate the performance of ', ],
[ 'the cat and bat \n\n\nBackground\n teamed up to find some food'],
['anthropology with BACKGROUND pharmacology and biology'],
])
testdf.columns = ['A']
testdf.head(10)
will return
A
0 generates the final summary. \nRESULTS We evaluate the performance of
1 the cat and bat \n\n\nRESULTS\n teamed up to find some food
2 anthropology with RESULTS pharmacology and biology
3 generates the final summary. \nMethods We evaluate the performance of
4 the cat and bat \n\n\nMETHODS\n teamed up to find some food
5 anthropology with METHODS pharmacology and biology
6 generates the final summary. \nBACKGROUND We evaluate the performance of
7 the cat and bat \n\n\nBackground\n teamed up to find some food
8 anthropology with BACKGROUND pharmacology and biology
And then
listStrings = { '\nRESULTS', '\nMETHODS' , '\nBACKGROUND' }
testdf.loc[testdf.A.apply(lambda x: len(listStrings.intersection(x.split())) >= 1)]
Will return nothing. The desired result would return the following rows.
A
0 generates the final summary. \nRESULTS We evaluate the performance of
1 the cat and bat \n\n\nRESULTS\n teamed up to find some food
4 the cat and bat \n\n\nMETHODS\n teamed up to find some food
6 generates the final summary. \nBACKGROUND We evaluate the performance of
These are rows where the word follows a '\n' and matches the case in the given set.