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 Overflow
🌐
DataScientYst
datascientyst.com › replace-regex-groups-in-pandas
How to Replace Regex Groups in Pandas
November 3, 2021 - In this short tutorial, we'll look at how to match and replace regex groups in Pandas. Here you can find the short answer: df_e['Date'].str.replace(r'(\d{2})/(\d{2})/(\d{4})', r"\3-\2-\1", regex=True) How to match and replace regex groups in
🌐
IncludeHelp
includehelp.com › python › using-regex-matched-groups-in-pandas-dataframe-replace-function.aspx
Python - Using regex matched groups in pandas dataframe replace function
October 6, 2023 - # Importing pandas package import pandas as pd # Importing numpy package import numpy as np # Creating DataFrame df = pd.DataFrame({'Name':['Raman,Pandey','Mohan,Bhargava']}) # Display Original dataframe print("Original DataFrame:\n",df,"\n") # Replacing values using regex df['Name'].replace({r'(\w+),\s+(\w+)' : r'\2 \1', 'Max':'Fritz'}, regex=True) # Print result print("Result:\n",df)
🌐
GitHub
github.com › softhints › Pandas-Tutorials › blob › master › regex › replace-regex-groups-in-pandas.ipynb
Pandas-Tutorials/regex/replace-regex-groups-in-pandas.ipynb at master · softhints/Pandas-Tutorials
"import pandas as pd\n", "\n", "cols = ['Date', 'Time', 'Latitude', 'Longitude', 'Depth', 'Magnitude Type']\n", "df_e = pd.read_csv(f'../data/earthquakes_1965_2016_database.csv.zip', dtype=str)[cols]\n", "\n", "df_e" ] }, { "cell_type": "markdown", "id": "a64e490b", "metadata": {}, "source": [ "## How to match and replace regex groups in Pandas" ] }, { "cell_type": "code", "execution_count": 3, "id": "d27939f6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 01/02/1965\n", "1 01/04/1965\n", "2 01/05/1965\n", "3 01/08/1965\n", "4 01/09/1965\n", "  ...
Author: softhints
🌐
DataScientYst
datascientyst.com › replace-values-regex-pandas
How to replace values with regex in Pandas
December 2, 2021 - In this quick tutorial, we'll show how to replace values with regex in Pandas DataFrame. There are several options to replace a value in a column or the whole DataFrame with regex: 1. Regex replace string df['applicants'].str.replace(r'\sapplicants', '') 2. Regex replace capture group df['applicants']
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.22 › generated › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 0.22.0 documentation
Using regex groups (extract second group and swap case): >>> pat = r"(?P<one>\w+) (?P<two>\w+) (?P<three>\w+)" >>> repl = lambda m: m.group('two').swapcase() >>> pd.Series(['One Two Three', 'Foo Bar Baz']).str.replace(pat, repl) 0 tWO 1 bAR dtype: object
🌐
Iditect
iditect.com › faq › python › using-regex-matched-groups-in-pandas-dataframe-replace-function.html
Using regex matched groups in pandas dataframe replace function
Let's assume you have a DataFrame named df with a column named 'text_column', and you want to replace a specific pattern in the strings within that column using regex matched groups: import pandas as pd # Sample DataFrame data = {'text_column': ['apple123', 'banana456', 'cherry789']} df = pd.DataFrame(data) # Use regex matched groups to replace a pattern in 'text_column' df['text_column'] = df['text_column'].replace(r'(\w+)(\d+)', r'\1_replaced_\2', regex=True) print(df)
🌐
Machine Learning Plus
machinelearningplus.com › blog › regex replace values using pandas
RegEx Replace values using Pandas - machinelearningplus
March 8, 2022 - In this article, we explain how to replace patterns using regex with examples · For using pandas replace function with regex, you need to define 3 parameters: to_replace, regex and value.
Find elsewhere
🌐
w3resource
w3resource.com › pandas › series › series-str-replace.php
Pandas Series: str.replace() function - w3resource
May 20, 2026 - Example - Using regex groups (extract second group and swap case): Python-Pandas Code: import numpy as np import pandas as pd pat = r"(?P<one>\w+) (?P<two>\w+) (?P<three>\w+)" repl = lambda m: m.group('two').swapcase() pd.Series(['One Two Three', 'Full Brr Bzz']).str.replace(pat, repl) Output: 0 tWO 1 bRR dtype: object ·
🌐
GeeksforGeeks
geeksforgeeks.org › replace-values-in-pandas-dataframe-using-regex
Replace values in Pandas dataframe using regex - GeeksforGeeks
July 31, 2023 - In this article, we will learn how we can replace values of a DataFrame with the value of another DataFrame using pandas. It can be done using the DataFrame.replace() method. It is used to replace a regex, string,  list, series, number, ...
🌐
Python Forum
python-forum.io › thread-2177.html
pandas dataframe.replace regex
Dear Pandas Experts, I am trying to replace occurences like 'United Kingdom of Great Britain and Ireland' or 'United Kingdom of Great Britain & Ireland' with just 'United Kingdom'. So I thought I use a regex to look for strings that contain 'United ...
🌐
Saturn Cloud
saturncloud.io › blog › how-to-use-regular-expressions-with-pandas-to-replace-values
Saturn Cloud | Saturn Cloud | The Control Plane for GPU Clouds
June 19, 2023 - Saturn Cloud is the white-labeled control plane for GPU clouds: multi-tenant isolation, day-2 support, and integrated billing, running in your cloud under your brand.