With sed, I can do
$ sed 's/^/prefix/' <<EOF
> foo
> EOF
prefixfoo
When I use pandas.Series.str.replace, the regex does not work. The minimal working example
import pandas as pd
df = pd.DataFrame({"text": ["foo", "bar"]})
df.text.str.replace("^", "prefix", regex=True)
returns
0 foo
1 bar
Name: text, dtype: object
CodePudding user response:
It works if you capture the ^
:
df['text'].str.replace(r'(^)', 'prefix', regex=True)
Output:
0 prefixfoo
1 prefixbar
Name: text, dtype: object
That said the best method to prepend a string would be to concatenate:
'prefix' df['text']