I think you have a few issues with the RegEx's.
As @Abdou just said use either '\\2 \\1' or better r'\2 \1', as '\1' is a symbol with ASCII code 1
Your solution should work if you will use correct RegEx's:
In [193]: df
Out[193]:
name
0 John, Doe
1 Max, Mustermann
In [194]: df.name.replace({r'(\w+),\s+(\w+)' : r'\2 \1'}, regex=True)
Out[194]:
0 Doe John
1 Mustermann Max
Name: name, dtype: object
In [195]: df.name.replace({r'(\w+),\s+(\w+)' : r'\2 \1', 'Max':'Fritz'}, regex=True)
Out[195]:
0 Doe John
1 Mustermann Fritz
Name: name, dtype: object
Answer from MaxU - stand with Ukraine on Stack OverflowI think you have a few issues with the RegEx's.
As @Abdou just said use either '\\2 \\1' or better r'\2 \1', as '\1' is a symbol with ASCII code 1
Your solution should work if you will use correct RegEx's:
In [193]: df
Out[193]:
name
0 John, Doe
1 Max, Mustermann
In [194]: df.name.replace({r'(\w+),\s+(\w+)' : r'\2 \1'}, regex=True)
Out[194]:
0 Doe John
1 Mustermann Max
Name: name, dtype: object
In [195]: df.name.replace({r'(\w+),\s+(\w+)' : r'\2 \1', 'Max':'Fritz'}, regex=True)
Out[195]:
0 Doe John
1 Mustermann Fritz
Name: name, dtype: object
setup
df = pd.DataFrame(dict(name=['Smith, Sean']))
print(df)
name
0 Smith, Sean
using replace
df.name.str.replace(r'(\w+),\s*(\w+)', r'\2 \1')
0 Sean Smith
Name: name, dtype: object
using extract
split to two columns
df.name.str.extract('(?P<Last>\w+),\s*(?P<First>\w+)', expand=True)
Last First
0 Smith Sean
You could use Series.str.replace:
import pandas as pd
df = pd.DataFrame(['$40,000*','$40000 conditions attached'], columns=['P'])
print(df)
# P
# 0 $40,000*
# 1 $40000 conditions attached
df['P'] = df['P'].str.replace(r'\D+', '', regex=True).astype('int')
print(df)
yields
P
0 40000
1 40000
since \D matches any character that is not a decimal digit.
You could use pandas' replace method; also you may want to keep the thousands separator ',' and the decimal place separator '.'
import pandas as pd
df = pd.DataFrame(['$40,000.32*','$40000 conditions attached'], columns=['pricing'])
df['pricing'].replace(to_replace="\$([0-9,\.]+).*", value=r"\1", regex=True, inplace=True)
print(df)
pricing
0 40,000.32
1 40000
Use a callable for repl
new_data = data.str.replace('(\d+[A-Z])', lambda m: m.group(1).lower())
Out[49]:
0 21st StNew York
1 Exampe BlvdSt Louis
2 1st Rd
dtype: object
We can try doing a regex replacement on the pattern (?<=\d)[A-Z], and then replacing with the lowercase version:
df['dat'] = df['data'].str.replace(r'(?<=\d)[A-Z]', lambda x: x.group(0).lower())
You could match as least word chars using \w*? and then capture in group 1 matching an optional A followed by BC (A?BC) followed by a word boundary.
\w*?(A?BC)\b
Regex demo
In there replacement use group 1
df.Col.str.replace(r'\w*?(A?BC)\b', r'\1')
You may a replace solution like:
df['Col'].str.replace(r'(?s)^.*?(A?BC)$', r'\1')
# 0 BC
# 1 ABC
Here, (?s).*?(A?BC)$ matches
(?s)- a.will match any char including line break chars^- start of string.*?- any 0+ chars, as few as possible(A?BC)- Group 1 (referred to with\1from the replacement pattern): an optionalAand thenBC$- end of string.