从一列 column 中的字符串中删除另一列中出现的单词的过程是什么?
例如:
Sr A B C
1 jack jack and jill and jill
2 run you should run, you should ,
3 fly you shouldnt fly,there you shouldnt ,there
可以看出,我想要C 列
,这样它就是B 减去A 的内容。请注意第三个示例,其中 fly
后跟一个逗号,因此它还应考虑标点符号(如果代码更倾向于检测其周围的空格)。
Column A
也可以有 2 个单词,所以这些需要被删除。
我需要 Pandas 中的表达式,例如:
df.apply(lambda x: x["C"].replace(r"\b"+x["A"]+r"\b", "").strip(), axis=1)
最佳答案
这看起来怎么样?
In [24]: df
Out[24]:
Sr A B
0 1 jack jack and jill
1 2 run you should run,
2 3 fly you shouldnt fly,there
[3 rows x 3 columns]
In [25]: df.apply(lambda row: row.B.strip(row.A), axis=1)
Out[25]:
0 and jill
1 you should run,
2 ou shouldnt fly,there
dtype: object
关于python - 删除其他列中出现的单词,Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22713441/