如何使用另一列删除未使用的字符串部分

时间:2018-04-24 09:54:14

标签: python pandas

如何在另一个Pandas列中删除未使用的字符串使用值部分?

我有:

Col1         Col2
bbbb2         Hello I want to removebbbb2

Output:
Hello I want to bbbb2

我想使用str.extract或任何其他pandas解决方案吗?

编辑: @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

您的解决方案仅适用于一个示例

2 个答案:

答案 0 :(得分:0)

例如,您可以将除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

编辑:对于多行,例如

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

答案 1 :(得分:0)

.applylambda

一起使用

<强>演示:

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))

<强>输出:

0    Hello I want to bbbb2
1    Hello I want to aaaa1
dtype: object