我有一个数据框架:
import pandas as pd
data =[[28, ['first'], 'apple edible', 23, 'apple is an edible fruit'],
[28, ['first'], 'apple edible', 34, 'fruit produced by an apple tree'],
[28, ['first'], 'apple edible', 39, 'the apple is a pome edible fruit'],
[21, ['second'], 'green plants', 11, 'plants are green'],
[21, ['second'], 'green plants', 7, 'plant these perennial green flowers']]
df = pd.DataFrame(data, columns=['day', 'group', 'bigram', 'count', 'sentence'])
+---+--------+------------+-----+-----------------------------------+
|day|group |bigram |count|sentence |
+---+--------+------------+-----+-----------------------------------+
|28 |[first] |apple edible|23 |apple is an edible fruit |
|28 |[first] |apple edible|34 |fruit produced by an apple tree |
|28 |[first] |apple edible|39 |the apple is a pome edible fruit |
|21 |[second]|green plants|11 |plants are green |
|21 |[second]|green plants|7 |plant these perennial green flowers|
+---+--------+------------+-----+-----------------------------------+
我需要找到一个句子中重字的交集。此外,首先查找交叉点并标记为True。也就是说,在第一个交叉点之后,其余的交叉点将被标记为False。语序不重要。
所以我想要这样的结果:
+---+--------+------------+-----+--------------------------------+--------+
|day|group |bigram |count|sentence | |
+---+--------+------------+-----+--------------------------------+--------+
|28 |[first] |apple edible|23 |apple is an edible fruit |True |
|28 |[first] |apple edible|34 |fruit produced by an apple tree |False |
|28 |[first] |apple edible|39 |the apple is a pome edible fruit|False |
|21 |[second]|green plants|11 |plant these perennial flowers |False |
|21 |[second]|green plants|7 |plants are green |True |
+---+--------+------------+-----+--------------------------------+--------+
首先通过将分割值转换为具有issubset
的集合来测试所有交集,然后每个bigram
只选择第一个True
s:
df['new'] = [set(b.split()).issubset(a.split()) for a,b in zip(df['sentence'],df['bigram'])]
df['new'] = ~df.duplicated(['bigram','new']) & df['new']
print (df)
day group bigram count sentence
0 28 [first] apple edible 23 apple is an edible fruit
1 28 [first] apple edible 34 fruit produced by an apple tree
2 28 [first] apple edible 39 the apple is a pome edible fruit
3 21 [second] green plants 11 plants are green
4 21 [second] green plants 7 plant these perennial green flowers
new
0 True
1 False
2 False
3 True
4 False
如果双字母顺序需要交换并且需要第一个交集使用:
df['new'] = ~df.assign(bigram=df['bigram'].apply(lambda x: frozenset(x.split()))).duplicated(['bigram','new']) & df['new']
您可以使用两个步骤,第一个步骤是识别bigram是句子子集的行(使用issubset
),然后只保留第一个True:
# use python sets to identify the matching bigrams
df['intersection'] = [set(a.split()).issubset(b.split())
for a,b in zip(df['bigram'], df['sentence'])]
# select the non-first matches and replace with False
df.loc[~df.index.isin(df.groupby(df['group'].str[0])['intersection'].idxmax()),
'intersection'] = False
输出:
day group bigram count sentence intersection
0 28 [first] apple edible 23 apple is an edible fruit True
1 28 [first] apple edible 34 fruit produced by an apple tree False
2 28 [first] apple edible 39 the apple is a pome edible fruit False
3 21 [second] green plants 11 plant these perennial green flowers False
4 21 [second] green plants 7 plants are green True