给出以下数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A' : ['a', 'b','c', 'd'],
'B' : ['and one', 'two','three', 'and four']
})
df
A B
0 a and one
1 b two
2 c three
3 d and four
我想剪掉'和'从任何以字符串的那一部分开头的单元格的开头。 期望的结果如下:
A B
0 a one
1 b two
2 c three
3 d four
提前致谢!
答案 0 :(得分:3)
您可以使用str.replace
的正则表达式:
>>> df
A B
0 a and one
1 b two
2 c three and
3 d and four
>>> df["B"] = df["B"].str.replace("^and ","")
>>> df
A B
0 a one
1 b two
2 c three and
3 d four
(请注意,我在第2行的末尾添加了"和"表示它不会被更改。)