Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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
Python re在数据帧中的应用_Python_Regex_Pandas - Fatal编程技术网

Python re在数据帧中的应用

Python re在数据帧中的应用,python,regex,pandas,Python,Regex,Pandas,!!目的是将工作方法应用于数据框中的文本 鉴于我有如下句子: “他邀请了两个人和三只宠物狗。” “她邀请了三个朋友和一只宠物猫。” 对于每一句话,我想在一个变量中计算有多少人被邀请,有多少宠物是宠物。这可以通过正则表达式轻松实现: sentence = 'He invited 2 people and pet 3 dogs.' human = [r'(\d+) people', r'(\d+) friend'] for h in human: number = re.search(h

!!目的是将工作方法应用于数据框中的文本

鉴于我有如下句子:

“他邀请了两个人和三只宠物狗。”

“她邀请了三个朋友和一只宠物猫。”

对于每一句话,我想在一个变量中计算有多少人被邀请,有多少宠物是宠物。这可以通过正则表达式轻松实现:

sentence = 'He invited 2 people and pet 3 dogs.'

human = [r'(\d+) people', r'(\d+) friend']

for h in human:
    number = re.search(h, sentence, re.IGNORECASE)
    if number is not None:
        number = number.group(1)

print('humans invited: ',number)

现在,这些句子位于“句子”列的数据框中。数据框中还有一列名为“人类”和一列名为“宠物”。我现在想用第一句话,像上面所示的那样处理它,把人类的结果写在“人类”栏中,对宠物也这样做,然后把它写在宠物栏中。但是,我不知道如何将其逐行应用于熊猫数据帧。

对于熊猫,您可以使用
str.extract
,例如:

df['humans'] = df['sentence'].str.extract('(\d+) (?:people|friend)', re.IGNORECASE, expand=False)

宠物和熊猫也一样,你可以使用str.extract,例如:

df['humans'] = df['sentence'].str.extract('(\d+) (?:people|friend)', re.IGNORECASE, expand=False)

宠物也一样

如果句子中只有两个数字,而你总是希望人类排在宠物前面,那么你可以一次得到所有数字:

df[['humans', 'pets']] = df.sentence.str.extract('(\d+).*?(\d+)', expand=True)
df
现在是:

                                          sentence humans    pets
0              He invited 2 people and pet 3 dogs.      2       3
1             She invited 3 friends and pet 1 cat.      3       1
2        She invited 13 friends and pet 145 frogs.     13     145
3  She invited 11243 friends and pet 141415 frogs.  11243  141415

如果句子中只有两个数字,而你总是希望
人类
出现在
宠物
之前,那么你可以一次获得所有数字:

df[['humans', 'pets']] = df.sentence.str.extract('(\d+).*?(\d+)', expand=True)
df
现在是:

                                          sentence humans    pets
0              He invited 2 people and pet 3 dogs.      2       3
1             She invited 3 friends and pet 1 cat.      3       1
2        She invited 13 friends and pet 145 frogs.     13     145
3  She invited 11243 friends and pet 141415 frogs.  11243  141415

你能举一个你想要的例子吗?如果我没弄错你的问题,你可能想看看。你能举个例子说明你想要什么吗?如果我没弄错你的问题,你可能想退房。