You're iterating through the elements within the DataFrame, in which case I'm assuming it's type str (or being coerced to str when you replace). str.replace doesn't have an argument for inplace=....

You should be doing this instead:

dataset['ver'] = dataset['ver'].str.replace('.', '')
Answer from r.ook on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 72198926 › why-pandas-replace-inplace-true-doesnt-work › 72199000
python - why pandas.replace inplace = True doesnt work - Stack Overflow
df2['D'][(df2['C'].isin(['cob','c']))].replace(3,5,inplace=True) df2['D'] and output is 3 only not 5 · 0 3.0 1 3.0 2 3.0 3 3.0 Name: D, dtype: float64 · can some one help me with this · python · python-3.x · pandas · dataframe · Share · Improve this question ·
Discussions

Python Pandas Commands .replace() Not Working despite inplace = True - Stack Overflow
I'm trying to use .replace() on a DataFrame made from .read_excel(). The command is not working despite using inplace = True, and triple-checking the spelling (copy pasted from original excel doc) ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to apply pandas.DataFrame.replace on selected columns with inplace = True? - Stack Overflow
I want to be able to apply the replace method on a subset of the columns specified by the user. I also want to use inplace = True to avoid making a copy of the dataframe, since it is huge. More on stackoverflow.com
🌐 stackoverflow.com
June 23, 2018
'in-place' string modifications in Python - Stack Overflow
In Python, strings are immutable. What is the standard idiom to walk through a string character-by-character and modify it? The only methods I can think of are some genuinely stanky hacks related... More on stackoverflow.com
🌐 stackoverflow.com
python - Why pandas DataFrame replace method does not work (inplace=True argument is used) - Stack Overflow
Add inplace=True to the accepted answer here: Replace all occurrences of a string in a pandas dataframe (Python) More on stackoverflow.com
🌐 stackoverflow.com
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.6 documentation
For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. ... If True, performs operation inplace...
🌐
Codecademy
codecademy.com › docs › python:pandas › dataframe › .replace()
Python:Pandas | DataFrame | .replace() | Codecademy
August 23, 2022 - ... dataframe is the DataFrame ... inplace is False by default. The original DataFrame values will not be replaced unless inplace is explicitly declared to True within the parameters....
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Replace values in DataFrame and Series with replace() | note.nkmk.me
January 17, 2024 - df = pd.read_csv('data/src/sample_pandas_normal.csv') print(df) # name age state point # 0 Alice 24 NY 64 # 1 Bob 42 CA 92 # 2 Charlie 18 CA 70 # 3 Dave 68 TX 70 # 4 Ellen 24 CA 88 # 5 Frank 30 NY 57 df.replace('CA', 'California', inplace=True) print(df) # name age state point # 0 Alice 24 NY 64 # 1 Bob 42 California 92 # 2 Charlie 18 California 70 # 3 Dave 68 TX 70 # 4 Ellen 24 California 88 # 5 Frank 30 NY 57
Find elsewhere
🌐
GitHub
github.com › pandas-dev › pandas › issues › 9106
DataFrame.replace with inplace=True fails when column names are not unique · Issue #9106 · pandas-dev/pandas
December 18, 2014 - data = pd.DataFrame.from_items([(0, ['a', 'b', 'c']), (1, ['1', '2', '3'])]) data.index=['<3', 'u', 'i'] index_order = ['i', '<3', 'u'] index_dict = dict([(index, order) for order, index in enumerate(index_order)]) data['index_rank'] = list(data.index) data['index_rank'].replace(index_dict, inplace=True) print(data)
Author: pandas-dev
🌐
pandas
pandas.pydata.org › pdeps › 0008-inplace-methods-in-pandas.html
pandas - Python Data Analysis Library
These methods don't operate inplace by default, but can be done inplace with inplace=True if the dtypes are compatible (e.g. the values replacing the old values can be stored in the original array without an astype).
🌐
W3Schools
w3schools.com › python › pandas › ref_df_replace.asp
Pandas DataFrame replace() Method
import pandas as pd data = { "name": ... newdf = df.replace(50, 60) Try it Yourself » · The replace() method replaces the specified value with another specified value....
🌐
Stack Overflow
stackoverflow.com › questions › 68878873 › python-pandas-how-to-apply-inplace-true-for-replace-data-and-get-updated-csv
python pandas how to apply inplace = True for replace data and get updated csv - Stack Overflow
1 Substituting values in a CSV file using python · 0 Replace values in a csv file · 2 How to apply pandas.DataFrame.replace on selected columns with inplace = True? 1 Replace value in existing column .csv pandas · 4 Pandas DataFrame replace does not work with inplace=True ·
🌐
datagy
datagy.io › home › pandas tutorials › pandas dataframes › pandas replace() – replace values in pandas dataframe
Pandas replace() - Replace Values in Pandas Dataframe • datagy
March 2, 2023 - # Replacing Values In Place df['Birth City'].replace( to_replace='Paris', value='France', inplace=True) print(df) # Returns: # Name Age Birth City Gender # 0 Jane 23 London F # 1 Melissa 45 France F # 2 John 35 Toronto M # 3 Matt 64 Atlanta M
🌐
Programiz
programiz.com › python-programming › pandas › methods › replace
Pandas replace()
Note: To learn more about Regular Expressions, please visit Python RegEx. import pandas as pd # create a DataFrame data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]} df = pd.DataFrame(data) # define a dictionary for replacement replacement_dict = {2: 200, 4: 400} # replace values using the dictionary df.replace(replacement_dict, inplace=True) print(df)
Top answer
1 of 11
135

When inplace=True is passed, the data is renamed in place (it returns nothing), so you'd use:

df.an_operation(inplace=True)

When inplace=False is passed (this is the default value, so isn't necessary), performs the operation and returns a copy of the object, so you'd use:

df = df.an_operation(inplace=False) 
2 of 11
130

In pandas, is inplace = True considered harmful, or not?

TLDR; Yes, yes it is.

  • inplace, contrary to what the name implies, often does not prevent copies from being created, and (almost) never offers any performance benefits
  • inplace does not work with method chaining
  • inplace can lead to SettingWithCopyWarning if used on a DataFrame column, and may prevent the operation from going though, leading to hard-to-debug errors in code

The pain points above are common pitfalls for beginners, so removing this option will simplify the API.


I don't advise setting this parameter as it serves little purpose. See this GitHub issue which proposes the inplace argument be deprecated api-wide.

It is a common misconception that using inplace=True will lead to more efficient or optimized code. In reality, there are absolutely no performance benefits to using inplace=True. Both the in-place and out-of-place versions create a copy of the data anyway, with the in-place version automatically assigning the copy back.

inplace=True is a common pitfall for beginners. For example, it can trigger the SettingWithCopyWarning:

df = pd.DataFrame({'a': [3, 2, 1], 'b': ['x', 'y', 'z']})

df2 = df[df['a'] > 1]
df2['b'].replace({'x': 'abc'}, inplace=True)
# SettingWithCopyWarning: 
# A value is trying to be set on a copy of a slice from a DataFrame

Calling a function on a DataFrame column with inplace=True may or may not work. This is especially true when chained indexing is involved.

As if the problems described above aren't enough, inplace=True also hinders method chaining. Contrast the working of

result = df.some_function1().reset_index().some_function2()

As opposed to

temp = df.some_function1()
temp.reset_index(inplace=True)
result = temp.some_function2()

The former lends itself to better code organization and readability.


Another supporting claim is that the API for set_axis was recently changed such that inplace default value was switched from True to False. See GH27600. Great job devs!

🌐
Reddit
reddit.com › r/learnpython › modifying a pandas dataframe inplace?
r/learnpython on Reddit: Modifying a pandas dataframe inplace?
November 8, 2023 -

Generally, you can do things to dataframes two ways in pandas:

df.<do_thing>(<args>, inplace=True)

or

df = df.<do_thing>(<args>)

My intuition is that the second way is much worse, because python essentially applies the transformation to a whole copy of df in memory, before then overwriting df. Whereas using 'inplace' sounds like it would instead do the thing in parts to the existing object in memory.

Is my intuition for these two syntaxes correct? If not, how can you truly modify a large dataframe inplace in memory without requiring double its size in memory to apply the operation?