熊猫:如何返回列有换行符/换行符(\ n)且紧随其后的几个区分大小写的单词之一的行?

时间:2019-06-17 02:50:00

标签: python pandas

这是此stackoverflow问题的后续操作

Pandas: How to return rows where a column has a line breaks/new line ( \n ) in its cell?

其中显示了如何获取换行符。

我现在想返回行,在该行中,列可以是换行之后紧随其后的几个区分大小写的单词之一。

这是一个最小的例子

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)

将返回

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

然后

listStrings = { '\nRESULTS',  '\nMETHODS' ,  '\nBACKGROUND' }
testdf.loc[testdf.A.apply(lambda x: len(listStrings.intersection(x.split())) >= 1)]

将不返回任何内容。 所需的结果将返回以下行。

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

在这些行中,单词后跟一个'\ n'并与给定集中的大小写匹配。

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

>>> testdf[testdf['A'].str.contains('\nRESULTS|\nMETHODS|\nBACKGROUND')]
                                                   A
0   generates the final summary. \nRESULTS We eva...
1  the cat and bat \n\n\nRESULTS\n teamed up to f...
4  the cat and bat \n\n\nMETHODS\n teamed up to f...
6   generates the final summary. \nBACKGROUND We ...
>>>