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
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 can't use date - timedelta, but you can use datetime - timedelta:
from datetime import datetime, timedelta
df['Date'] = datetime.datetime.today() - df.Date.str.extract('Posted: (\d+) days? ago')[0].astype(int).apply(timedelta)
Output:
>>> df
Date Code Value
0 2021-12-22 08:33:03.396630 xa01332cs 101
1 2021-12-21 08:33:03.396630 d11as99101 630
2 2021-12-12 08:33:03.396630 12011rww1a 301
You can extract the numbers, convert it to timedelta, then subtract:
df['New Date'] = datetime.datetime.today() - df['Date'].str.extract(r"Posted: (\d+) days? ago").astype(int) * pd.Timedelta('1D')
Output:
Date Code Value New Date
0 Posted: 1 day ago xa01332cs 101 2021-12-22 10:36:13.361973
1 Posted: 2 days ago d11as99101 630 2021-12-21 10:36:13.361973
2 Posted: 11 days ago 12011rww1a 301 2021-12-12 10:36:13.361973
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
I think you can split it instead of using RegEx:
In [170]: s
Out[170]:
0 SC_S193_M7.CONTROLDAY10.EPI.P1_Stem
1 SC_S194_M7.CONTROLDAY10.EPI.P1_Goblet
2 SC_S102_M1.CONTROLDAY3.EPI2_Enterocyte
3 SC_S106_M1.CONTROLDAY3.EPI2_Goblet
Name: 0, dtype: object
In [171]: s.str.split('_').str[-1]
Out[171]:
0 Stem
1 Goblet
2 Enterocyte
3 Goblet
Name: 0, dtype: object
or better using rsplit(..., n=1):
In [174]: s.str.rsplit('_', n=1).str[-1]
Out[174]:
0 Stem
1 Goblet
2 Enterocyte
3 Goblet
Name: 0, dtype: object
alternatively you can use .str.extract():
In [177]: s.str.extract(r'.*_([^_]*)$', expand=False)
Out[177]:
0 Stem
1 Goblet
2 Enterocyte
3 Goblet
Name: 0, dtype: object
Another variant (assuming that s is your series) that should work is something along the lines of
s.apply(lambda r : re.sub('.*_([^_]*)$', '\\1', r))