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
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 3.0.5 documentation
Method to replace occurrences of a substring with another substring. ... Extract substrings using a regular expression. ... Find all occurrences of a pattern or regex in each string.
🌐
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
🌐
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
🌐
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 - Suppose we are given the dataframe ... rows. ... To replace a value using regex or by comparing the value by regex, we will use "df[col].str.replce() method, inside which we will define our regex to compare....
🌐
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
🌐
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 ·
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas documentation
Method to replace occurrences of a substring with another substring. ... Extract substrings using a regular expression. ... Find all occurrences of a pattern or regex in each string.
🌐
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']
Find elsewhere
🌐
Machine Learning Plus
machinelearningplus.com › blog › regex replace values using pandas
RegEx Replace values using Pandas - machinelearningplus
March 8, 2022 - These may include retrieving hashtags from a tweet, extracting dates from a text, or removing website links. Pandas replace() function is used to replace a string regex, list, dictionary, series, number in a dataframe.
🌐
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 are going to see how to replace characters in strings in pandas dataframe using Python. We can replace characters using str.replace() method is basically replacing an existing string or character in a string with a new ...
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › Series › str › replace
Python Pandas Series str replace() - Replace Substring | Vultr Docs
November 26, 2024 - replacements = {'foo': 'new', 'bar': 'old'} modified_data = data.str.replace('|'.join(replacements.keys()), lambda m: replacements[m.group(0)], regex=True) print(modified_data) Explain Code
🌐
Skytowner
skytowner.com › explore › pandas_series_str_replace_method
Pandas Series str | replace method with Examples
The function that you pass to repl is given one argument - the regex object that captures the match. This function must return a string that will replace the match. ... Here, we are replacing every string that ends with A by its uppercase form. Just to clarify, foo is called twice in this case and if we were to print the value of match, we would see the following: ... Ask a question or leave a feedback... Official Pandas Documentation https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.replace.html
🌐
Programiz
programiz.com › python-programming › pandas › methods › series-str-replace
Pandas str.replace() (With Examples)
import pandas as pd # create a Series products = pd.Series(['T-shirt 12', 'Jeans 30', 'Hat', 'Dress 8', 'Shoes 42']) # use str.replace() with regex to replace numeric sizes products_replaced = products.str.replace(r'\d+', 'SIZE', regex=True) print(products_replaced)