Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使FOR循环中的IF在数据帧上工作?python_Python_Pandas_For Loop_If Statement_Dataframe - Fatal编程技术网

如何使FOR循环中的IF在数据帧上工作?python

如何使FOR循环中的IF在数据帧上工作?python,python,pandas,for-loop,if-statement,dataframe,Python,Pandas,For Loop,If Statement,Dataframe,我试图根据词组的起始词对来选择句子的段/从句。例如,我对以“what does”或“what is”开头的句子片段感兴趣 为此,我在两个数据帧上循环,在for循环中使用if语句,如下所示。第一个数据帧df1['statement']包含这些句子。另一个df2['First2']包含起始词对。但是,该函数似乎只在for循环中的第一个词对上循环——在第一项之后,它不会返回for循环。当我将列表传递给它时,我的代码似乎工作,但当我传递数据帧时,我的代码不工作。我尝试了中提到的解决方案。但它们不工作对于

我试图根据词组的起始词对来选择句子的段/从句。例如,我对以“what does”或“what is”开头的句子片段感兴趣

为此,我在两个数据帧上循环,在
for循环中使用
if语句
,如下所示。第一个数据帧
df1['statement']
包含这些句子。另一个
df2['First2']
包含起始词对。但是,该函数似乎只在
for循环中的第一个词对上循环
——在第一项之后,它不会返回for循环。当我将列表传递给它时,我的代码似乎工作,但当我传递数据帧时,我的代码不工作。我尝试了中提到的解决方案。但它们不工作对于我的数据帧,我很想知道如何解决这个问题

数据帧:

   'Sentence'                                   'First2'     
0  If this is a string what does it say?      0  what does    
1  And this is a string, should it say more?  1  should it    
2  This is yet another string.                2
我的代码如下所示:

import pandas as pd    
a = df1['Sentence']
b = df2['First2'] 

#The function seems to loop over all r's but not over all b's:
def func(r): 
    for i in b:
        if i in r:
            # The following line selects the sentence segment that starts with 
            # the words in `First2`, up to the end of the sentence.
            q = r[r.index(i):] 
            return q
        else:
            return ''

df1['Clauses'] = a.apply(func)
结果是:

what does it say?
这是正确的,但不完整。代码似乎覆盖了所有
r
,但没有覆盖所有
b
。如何获得所需的结果,如下所示

what does it say?
should it say more?

我不确定我是否理解正确,但看起来您希望存储
'First2'
中的所有短语(例如,在变量
s
中),并有一列
'Clauses'
,该列是与
s
中包含的任何短语匹配后字符串的剩余部分

可能有一种更有效的方法,但这里有一种用正则表达式实现这一点的方法:

# build the capturing string
s = '(' + '|'.join(df.First2[df.First2 != ''].values + '.*') + ')'
# use the pandas Series.str method to extract, and assign to new column
df['Clauses'] = df.Sentence.str.extract(s, expand = False)

这段代码回答了我的问题:

import pandas as pd    
a = df1['Sentence']
b = df2['First2'] 

def func(r):
    for i in b:
        if i in r:
            q = r[r.index(i):]
            return q
    return ''

df['Segments'] = a.apply(func)

卢大明在这里指出:希望这有助于其他人。

使用
如果r中的i:
中为b中的i:
i
设置为on evalue,然后将其更改为另一个-尝试不同的变量名?在循环的第一次迭代中总是返回
q
或空字符串。你永远看不到seco
b
@doctorlove的第二个元素:但我想知道的是
I
是否同时存在于
b
r
中……如果我在其中一个语句中将
I
更改为例如
x
,这会起作用吗?@Evert:是的,这似乎是正确的。但如何更改呢?@Evert:已经纠正了你的问题。我会的希望有一个列
子句
,包含从前两个单词开始一直到句子结尾的句子段。该段还需要包括前两个单词。我已经更新了代码中的注释,希望能让这更清楚。我想你的答案是选择任何符合t保留
First2
中的单词,保留单词本身。但我希望它们输入,而且我希望选择区分大小写。因此,如果
First2
是小写的,则只应选择单词为小写的段。您是否确实尝试过在我的答案中运行代码?当然可以。但它没有给出d我在下面发布了解决方案。我的原始代码中有一个错误。