Pandas检查字符串列是否包含一对字符串

2024-04-28 09:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有这样一个数据帧:

df = pd.DataFrame({'consumption':['squirrel eats apple', 'monkey eats apple', 
                                  'monkey eats banana', 'badger eats banana'], 
                   'food':['apple', 'apple', 'banana', 'banana'], 
                   'creature':['squirrel', 'badger', 'monkey', 'elephant']})

    consumption creature    food
0   squirrel eats apple squirrel    apple
1   monkey eats apple   badger  apple
2   monkey eats banana  monkey  banana
3   badger eats banana  elephant    banana

我想在“消费”列中找到“生物”和“食物”组合出现的行,即如果苹果和松鼠同时出现,则为真,但如果苹果与大象同时出现,则为假。同样,如果Monkey&Banana同时出现,则为True,但Monkey Apple则为false。在

我尝试的方法是:

^{pr2}$

但这行不通,因为我在所有情况下都是这样。在

如何检查字符串对?在


Tags: 数据苹果appledffoodmonkeypdbanana
3条回答

有一种可能的方法:

def match_consumption(r):
    if (r['creature'] in r['consumption']) and (r['food'] in r['consumption']):
        return True
    else:
        return False

df['match'] = df.apply(match_consumption, axis=1)
df

           consumption  creature    food  match
0  squirrel eats apple  squirrel   apple   True
1    monkey eats apple    badger   apple  False
2   monkey eats banana    monkey  banana   True
3   badger eats banana  elephant  banana  False

检查字符串相等是否太简单了?您可以测试字符串<creature> eats <food>是否等于consumption列中的相应值:

(df.consumption == df.creature + " eats " + df.food)

我相信有更好的办法。但这是一种方法。在

import pandas as pd
import re

df = pd.DataFrame({'consumption':['squirrel eats apple', 'monkey eats apple', 'monkey eats banana', 'badger eats banana'], 'food':['apple', 'apple', 'banana', 'banana'], 'creature':['squirrel', 'badger', 'monkey', 'elephant']})

test = []
for i in range(len(df.consumption)):
    test.append(bool(re.search(df.creature[i],df.consumption[i])) & bool((re.search(df.food[i], df.consumption[i]))))
df['test'] = test

相关问题 更多 >