Python 字符串包含两个熊猫系列
我有一个系列,数据框中有一些字符串。我想搜索相邻列中是否存在该字符串 在下面的示例中,我想搜索“choice”系列中的字符串是否包含在“fruit”系列中,在新列“choice_match”中返回true(1)或false(0) 数据帧示例:Python 字符串包含两个熊猫系列,python,string,pandas,dataframe,Python,String,Pandas,Dataframe,我有一个系列,数据框中有一些字符串。我想搜索相邻列中是否存在该字符串 在下面的示例中,我想搜索“choice”系列中的字符串是否包含在“fruit”系列中,在新列“choice_match”中返回true(1)或false(0) 数据帧示例: import pandas as pd d = {'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'fruit': [ 'apple, banana', 'apple', 'apple', 'pineapple', 'appl
import pandas as pd
d = {'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'fruit': [
'apple, banana', 'apple', 'apple', 'pineapple', 'apple, pineapple', 'orange', 'apple, orange', 'orange', 'banana', 'apple, peach'],
'choice': ['orange', 'orange', 'apple', 'pineapple', 'apple', 'orange', 'orange', 'orange', 'banana', 'banana']}
df = pd.DataFrame(data=d)
import pandas as pd
d = {'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'fruit': [
'apple, banana', 'apple', 'apple', 'pineapple', 'apple, pineapple', 'orange', 'apple, orange', 'orange', 'banana', 'apple, peach'],
'choice': ['orange', 'orange', 'apple', 'pineapple', 'apple', 'orange', 'orange', 'orange', 'banana', 'banana'],
'choice_match': [0, 0, 1, 1, 1, 1, 1, 1, 1, 0]}
df = pd.DataFrame(data=d)
所需数据帧:
import pandas as pd
d = {'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'fruit': [
'apple, banana', 'apple', 'apple', 'pineapple', 'apple, pineapple', 'orange', 'apple, orange', 'orange', 'banana', 'apple, peach'],
'choice': ['orange', 'orange', 'apple', 'pineapple', 'apple', 'orange', 'orange', 'orange', 'banana', 'banana']}
df = pd.DataFrame(data=d)
import pandas as pd
d = {'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'fruit': [
'apple, banana', 'apple', 'apple', 'pineapple', 'apple, pineapple', 'orange', 'apple, orange', 'orange', 'banana', 'apple, peach'],
'choice': ['orange', 'orange', 'apple', 'pineapple', 'apple', 'orange', 'orange', 'orange', 'banana', 'banana'],
'choice_match': [0, 0, 1, 1, 1, 1, 1, 1, 1, 0]}
df = pd.DataFrame(data=d)
逐步:
In [78]: df['fruit'].str.split(',\s*', expand=True)
Out[78]:
0 1
0 apple banana
1 apple None
2 apple None
3 pineapple None
4 apple pineapple
5 orange None
6 apple orange
7 orange None
8 banana None
9 apple peach
In [79]: df['fruit'].str.split(',\s*', expand=True).eq(df['choice'], axis=0)
Out[79]:
0 1
0 False False
1 False False
2 True False
3 True False
4 True False
5 True False
6 False True
7 True False
8 True False
9 False False
In [80]: df['fruit'].str.split(',\s*', expand=True).eq(df['choice'], axis=0).any(1)
Out[80]:
0 False
1 False
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 False
dtype: bool
In [81]: df['fruit'].str.split(',\s*', expand=True).eq(df['choice'], axis=0).any(1).astype(np.int8)
Out[81]:
0 0
1 0
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 0
dtype: int8
这里有一个方法:
df['choice_match'] = df.apply(lambda row: row['choice'] in row['fruit'].split(','),\
axis=1).astype(int)
解释
和df.apply
在每行循环并应用逻辑;它接受匿名axis=1
函数lambda
从row['fruit'].split(',')
列创建一个列表。这是必要的,例如,fruit
中不考虑苹果菠萝
是将布尔值转换为整数以便于显示的必要条件astype(int)
使用Numpy的
查找
当find
没有找到值时,它返回-1
from numpy.core.defchararray import find
choice = df.choice.values.astype(str)
fruit = df.fruit.values.astype(str)
df.assign(choice_match=(find(fruit, choice) > -1).astype(np.uint))
ID choice fruit choice_match
0 1 orange apple, banana 0
1 2 orange apple 0
2 3 apple apple 1
3 4 pineapple pineapple 1
4 5 apple apple, pineapple 1
5 6 orange orange 1
6 7 orange apple, orange 1
7 8 orange orange 1
8 9 banana banana 1
9 10 banana apple, peach 0
选项2
设置逻辑
使用set
s嗯,找到一种有趣的方法获取假人
(df.fruit.str.replace(' ','').str.get_dummies(',')+df.choice.str.get_dummies()).gt(1).any(1)
Out[726]:
0 False
1 False
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 False
dtype: bool
在把它分配回来之后
df['New']=(df.fruit.str.replace(' ','').str.get_dummies(',')+df.choice.str.get_dummies()).gt(1).any(1).astype(int)
df
Out[728]:
ID choice fruit New
0 1 orange apple, banana 0
1 2 orange apple 0
2 3 apple apple 1
3 4 pineapple pineapple 1
4 5 apple apple, pineapple 1
5 6 orange orange 1
6 7 orange apple, orange 1
7 8 orange orange 1
8 9 banana banana 1
9 10 banana apple, peach 0
谢谢你的解释,非常有帮助:)我决定在我的答案中添加一个选项,这个选项是受此启发的。回答得好!
df['New']=(df.fruit.str.replace(' ','').str.get_dummies(',')+df.choice.str.get_dummies()).gt(1).any(1).astype(int)
df
Out[728]:
ID choice fruit New
0 1 orange apple, banana 0
1 2 orange apple 0
2 3 apple apple 1
3 4 pineapple pineapple 1
4 5 apple apple, pineapple 1
5 6 orange orange 1
6 7 orange apple, orange 1
7 8 orange orange 1
8 9 banana banana 1
9 10 banana apple, peach 0