The issue with some of the other answers is that they don't work with all Dataframes, only with Series, or Dataframes that can be implicitly converted to a Series. I understand this is because the .str construct exists in the Series class, but not in the Dataframe class.

To work with Dataframes, you can make your regular expression case insensitive with the (?i) extension. I don't believe this is available in all flavors of RegEx but it works with Pandas.

d = {'a':['test', 'Test', 'cat'], 'b':['CAT', 'dog', 'Cat']}
df = pd.DataFrame(data=d)

    a       b
0   test    CAT
1   Test    dog
2   cat     Cat

Then use replace as you normally would but with the (?i) extension:

df.replace('(?i)cat', 'MONKEY', regex=True)

    a       b
0   test    MONKEY
1   Test    dog
2   MONKEY  MONKEY
Answer from geekly on Stack Overflow
Top answer
1 of 1
12

The case argument is actually a convenience as an alternative to specifying flags=re.IGNORECASE. It has no bearing on replacement if the replacement is not regex-based.

So, when regex=True, these are your possible choices:

pd.Series('Jr. eng').str.replace(r'jr\.', 'jr', regex=True, case=False)
# pd.Series('Jr. eng').str.replace(r'jr\.', 'jr', case=False)

0    jr eng
dtype: object

Or,

pd.Series('Jr. eng').str.replace(r'jr\.', 'jr', regex=True, flags=re.IGNORECASE)
# pd.Series('Jr. eng').str.replace(r'jr\.', 'jr', flags=re.IGNORECASE)

0    jr eng
dtype: object

You can also get cheeky and bypass both keyword arguments by incorporating the case insensitivity flag as part of the pattern as ?i. See

pd.Series('Jr. eng').str.replace(r'(?i)jr\.', 'jr')
0    jr eng
dtype: object

Note
You will need to escape the period \. in regex mode, because the unescaped dot is a meta-character with a different meaning (match any character). If you want to dynamically escape meta-chars in patterns, you can use re.escape.

For more information on flags and anchors, see this section of the docs and re HOWTO.


From the source code, it is clear that the "case" argument is ignored if regex=False. See

# Check whether repl is valid (GH 13438, GH 15055)
if not (is_string_like(repl) or callable(repl)):
    raise TypeError("repl must be a string or callable")

is_compiled_re = is_re(pat)
if regex:
    if is_compiled_re:
        if (case is not None) or (flags != 0):
            raise ValueError("case and flags cannot be set"
                             " when pat is a compiled regex")
    else:
        # not a compiled regex
        # set default case
        if case is None:
            case = True

        # add case flag, if provided
        if case is False:
            flags |= re.IGNORECASE
    if is_compiled_re or len(pat) > 1 or flags or callable(repl):
        n = n if n >= 0 else 0
        compiled = re.compile(pat, flags=flags)
        f = lambda x: compiled.sub(repl=repl, string=x, count=n)
    else:
        f = lambda x: x.replace(pat, repl, n)

You can see the case argument is only checked inside the if statement.

IOW, the only way is to ensure regex=True so that replacement is regex-based.

Discussions

python - pandas "case insensitive" in a string or "case ignore" - Stack Overflow
Series.str.contains has a case parameter that is True by default. Set it to False to do a case insensitive match. More on stackoverflow.com
🌐 stackoverflow.com
Any way to have pandas remove_duplicates and merge to be case insensitive without changing the original values?
I'd add a casefolded column, then drop dupes based on that column. df["TheColumnFolded"] = df["TheColumn"].astype(str).str.casefold() result_df = df.drop_duplicates(subset=["TheColumnFolded"], keep="first") result_df.drop(["TheColumnFolded"], axis=1, inplace=True) This may contains typos. More on reddit.com
🌐 r/learnpython
2
5
July 16, 2024
pandas - Python: replace case insensitive flag doesn't work - Stack Overflow
In my dataframe I want to replace different ways of representing something with a single consistent string. Examples: Replace [COM, COMMERCIAL] with "Commercial". Replace [FALSE, False, ... More on stackoverflow.com
🌐 stackoverflow.com
python - What's the easiest way to do a case-insensitive string replacement in Pandas? - Stack Overflow
Is there a way to get a case insensitive regex replace to work on a Pandas dataframe? I would prefer to keep the work vectorised instead of having to resort to creating a loop at the string level to More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 73747879 › case-insensitive-pandas-series-replace
python - case insensitive pandas.Series.replace - Stack Overflow
What is the best method for replacing values in a case-insensitive manner while maintaining the same categories (in the same order)? import pandas as pd import numpy as np # set up a DF with ordered categories values = ['one','two','three','na','Na','NA'] df = pd.DataFrame({ 'categ' : values }) df['categ'] = df['categ'].astype('category') df['categ'].cat.categories = values # replace values df['categ'].replace( to_replace='na', value=np.nan )
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 3.0.6 documentation
Determines if replace is case sensitive: If True, case sensitive (the default if pat is a string) Set to False for case insensitive · Cannot be set if pat is a compiled regex. flagsint, default 0 (no flags) Regex module flags, e.g. re.IGNORECASE. Cannot be set if pat is a compiled regex.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.0 › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 0.25.0 documentation
Pandas arrays · Panel · Index objects · Date offsets · Frequencies · Window · GroupBy · Resampling · Style · Plotting · General utility functions · Extensions · Development · Release Notes · Enter search terms or a module, class or function name. Series.str.replace(self, pat, repl, n=-1, case=None, flags=0, regex=True)[source]¶ ·
🌐
Plus2Net
plus2net.com › python › pandas-str-replace.php
replace() to Replace all or some occurrence of matching string in DataFrame
February 5, 2019 - import pandas as pd my_dict={'...e('@','#')) Output · 0 Ravi#example.com 1 Raju#example.com 2 Alex#example.com · By using option case=False we can make case insensitive search and replace....
🌐
Reddit
reddit.com › r/learnpython › any way to have pandas remove_duplicates and merge to be case insensitive without changing the original values?
r/learnpython on Reddit: Any way to have pandas remove_duplicates and merge to be case insensitive without changing the original values?
July 16, 2024 -

Howdy!

I have a code that takes a dataframe and generates a Dimension Table out of its columns. A dimension table is a dataframe that contains only distinct values and an associated ID. This table is then merged back to the original table so it replaces the original value by its ID.

I'm having an issue where df.remove_duplicates() and pd.merge() considers the same string in different capitalizations different things ("A" and "a" are different). When loading this data to PowerBI, PowerBI considers them the same ("A" and "a" are equivalent, and considered as duplicates)

Since I'm dealing with a large table I'm bound to have this happen on multiple columns. Is there any way that I can make python ignore capitalization when performing a remove_duplicates or a merge? That way I'd have either capitalization (either "A" or "a" is fine) but only for the cases in which there are duplicates with different capitalizations.

I'd like to avoid making changes to the original values if possible, since my current solution is to just make everything uppercase and I'd like to avoid that.

Find elsewhere
🌐
YouTube
youtube.com › watch
How to Perform Case Insensitive Value Replacement in Pandas Series - YouTube
Learn how to effectively replace values in a Pandas Series in a `case insensitive` manner while preserving categories.---This video is based on the question ...
Published: April 7, 2025
Views: 0
🌐
GitHub
github.com › cangermueller › cheat › blob › master › python › pandas.txt
cheat/python/pandas.txt at master · cangermueller/cheat
* col.str.replace(r'.*_m-([^_]+).*', '\\1') case=False // case insensitive · slice_replace(from, to, replace) slice(from, to) pad(length, fillchar=' ') // extend length · startswith(string) // starts with string (not re!) fullmatch(re, ...) // matches full string ·
Author: cangermueller
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › Series › str › replace
Python Pandas Series str replace() - Replace Substring | Vultr Docs
November 26, 2024 - By default, replacements are case-sensitive. Use the flags parameter with re.IGNORECASE for case-insensitive replacements.
🌐
Statology
statology.org › home › how to use str.replace in pandas (with examples)
How to Use str.replace in Pandas (With Examples)
April 11, 2024 - By specifying case=False, we are able to replace each occurrence of “Mavs” in the team column with “Thunder”, regardless of case. If you would like to replace multiple patterns with a new string, then you can use the | operator along ...
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas documentation
Determines if replace is case sensitive: If True, case sensitive (the default if pat is a string) Set to False for case insensitive · Cannot be set if pat is a compiled regex. flagsint, default 0 (no flags) Regex module flags, e.g. re.IGNORECASE. Cannot be set if pat is a compiled regex.
🌐
Stack Overflow
stackoverflow.com › questions › 40596385 › whats-the-easiest-way-to-do-a-case-insensitive-string-replacement-in-pandas
python - What's the easiest way to do a case-insensitive string replacement in Pandas? - Stack Overflow
Does the regular string .str accessor str.replace not work? It has a case parameter that can be set to False... ... So then str.replace does take ignorecase arguments!
🌐
Programiz
programiz.com › python-programming › pandas › methods › series-str-replace
Pandas str.replace() (With Examples)
Original Series: 0 apple 1 banana 2 cherry dtype: object Replace first occurrence: 0 @pple 1 b@nana 2 cherry dtype: object Replace first two occurrences: 0 @pple 1 b@n@na 2 cherry dtype: object No replacement (n=0): 0 apple 1 banana 2 cherry dtype: object ... # case insensitive replacement case_insensitive_replace = data.str.replace('a', '@', case=False) print("\nCase Sensitive Replacement:") print(case_sensitive_replace) print("\nCase Insensitive Replacement:") print(case_insensitive_replace)
🌐
HAR Data Extractor
jonathansoma.com › course › foundations-2021 › replacing-with-str-replace-and-replace
Replacing with .str.replace and .replace
Notice how "I love potatoes" is still about potatoes and not chocolate. If you want pandas to ignore case while replacing strings, use case=False. df['edited'] = df.original.str.replace("Potatoes", "Chocolate", case=False) df · You cannot make replace case-insensitive (unless you work with ...
🌐
DataScientYst
datascientyst.com › pandas-contains-using-case-insensitive-search
Pandas: Contains Using Case Insensitive Search
May 16, 2025 - import pandas as pd df = pd.DataFrame({ 'name': ['maximum', 'Maxxy', 'MAXa', 'Mini', 'MInimum', 'MaX'] }) # Filter rows where 'name' contains 'bob', ignoring case filtered = df[df['name'].str.contains('max', case=False)] print(filtered) ... For non-regex comparisons, normalize both sides with .str.lower() or .str.upper(). To make your string filters case-insensitive in Pandas:
🌐
GitHub
github.com › rapidsai › cudf › issues › 5217
[FEA] Ignore case when using regex in replace · Issue #5217 · rapidsai/cudf
May 18, 2020 - import pandas as pd s = pd.Series(['A','B']) print(s.str.replace(r'(?i)a', 'C')) 0 C 1 B dtype: object · import cudf s = cudf.Series(['A','B']) print(s.str.replace(r'(?i)a', 'C')) 0 A 1 B dtype: object ... Describe alternatives you've considered For chars, it makes sense to use 'a' and 'A' in the regex, but not for strings because a combination of all lower and upper cases must be created.
Author: rapidsai