[英]How to remove unused part of string using another column
How to remove unused part of string used value in another Pandas column? 如何在另一个Pandas列中删除字符串使用值的未使用部分?
I have: 我有:
Col1 Col2
bbbb2 Hello I want to removebbbb2
Output:
Hello I want to bbbb2
I want to use str.extract or any other pandas solution? 我想使用str.extract或其他任何熊猫解决方案?
EDIT: @erocoar But i want to remove in all rows, For example 编辑:@erocoar但是我想在所有行中删除,例如
Col1 Col2 output
bbbb2 Hello I want to removebbbb2 Hello I want to bbbb2
aaaa1 Hello I want to remaaaa1 Hello I want to aaaa1
Your solution is only for one example 您的解决方案仅是一个示例
You could for example sub out all the characters other than bbbb2
. 例如,您可以bbbb2
以外的所有其他bbbb2
。
df = pd.DataFrame(data={"Col1": ["bbbb2"], "Col2": ["Hello I want to removebbbb2"]})
df["Col2"].str.replace("(?:\S*)?(bbbb2)(?:\S*)?", "\\1")
Out[29]:
0 Hello I want to bbbb2
Name: Col2, dtype: object
Edit: For multiple rows, eg 编辑:对于多行,例如
import re
df = pd.DataFrame(data={"Col1": ["bbbb2", "aaaa1"], "Col2": ["Hello I want to removebbbb2", "Hello I want to remaaaa1"]})
df["out"] = df.apply(lambda x: re.sub("(?:\S*)?(" + x[0] + ")(?:\S*)?", "\\1", x[1]), axis=1)
df
Out[127]:
Col1 Col2 out
0 bbbb2 Hello I want to removebbbb2 Hello I want to bbbb2
1 aaaa1 Hello I want to remaaaa1 Hello I want to aaaa1
Using .apply
with lambda
在lambda
使用.apply
Demo: 演示:
import pandas as pd
df = pd.DataFrame({"Col1":["bbbb2", "aaaa1"], "Col2":["Hello I want to removebbbb2", "Hello I want to remaaaa1"]})
def rep(row):
s = row["Col2"].split()
s[-1] = row["Col1"]
return " ".join(s)
print(df.apply(lambda row: rep(row), axis=1))
Output: 输出:
0 Hello I want to bbbb2
1 Hello I want to aaaa1
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.