Many ways to do that

1

In [7]: d.sales[d.sales==24] = 100

In [8]: d
Out[8]: 
   day     flavour  sales  year
0  sat  strawberry     10  2008
1  sun  strawberry     12  2008
2  sat      banana     22  2008
3  sun      banana     23  2008
4  sat  strawberry     11  2009
5  sun  strawberry     13  2009
6  sat      banana     23  2009
7  sun      banana    100  2009

2

In [26]: d.loc[d.sales == 12, 'sales'] = 99

In [27]: d
Out[27]: 
   day     flavour  sales  year
0  sat  strawberry     10  2008
1  sun  strawberry     99  2008
2  sat      banana     22  2008
3  sun      banana     23  2008
4  sat  strawberry     11  2009
5  sun  strawberry     13  2009
6  sat      banana     23  2009
7  sun      banana    100  2009

3

In [28]: d.sales = d.sales.replace(23, 24)

In [29]: d
Out[29]: 
   day     flavour  sales  year
0  sat  strawberry     10  2008
1  sun  strawberry     99  2008
2  sat      banana     22  2008
3  sun      banana     24  2008
4  sat  strawberry     11  2009
5  sun  strawberry     13  2009
6  sat      banana     24  2009
7  sun      banana    100  2009
Answer from waitingkuo on Stack Overflow
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.replace.html
pandas.DataFrame.replace โ€” pandas 3.0.6 documentation
For a DataFrame a dict can specify that different values should be replaced in different columns. For example, {'a': 1, 'b': 'z'} looks for the value 1 in column โ€˜aโ€™ and the value โ€˜zโ€™ in column โ€˜bโ€™ and replaces these values with whatever is specified in value.
Discussions

pandas - Conditionally replace dataframe cells with value from another cell - Data Science Stack Exchange
I have a couple pandas data frame questions. I would like to replace the values in only certain cells (based on a boolean condition) with a value identified from another cell. I have defined the data More on datascience.stackexchange.com
๐ŸŒ datascience.stackexchange.com
January 15, 2020
Pandas: replace single cell in data frame: variable assignment not in place? what can I do?
For a start, you should never be iterating through anything using range(len(whatever)). Forget that you ever learned that. It is never correct. Usually you would iterate through the thing itself: for row in data - but that is not the right thing to do with Pandas. There is rarely a need to iterate a dataframe, and there definitely isn't here. You should assign directly: data.loc[(data['Sex'] == '1') | (data['Sex'].str.contains('f')), 'Sex'] = 'f' data.loc[data['Sex'] != 'f', 'Sex'] = 'm' or with apply and a lambda with a conditional expression: data['Sex'] = data['Sex'].apply(lambda sex: 'f' if sex == '1' or 'f' in sex else 'm') More on reddit.com
๐ŸŒ r/learnpython
4
1
December 2, 2022
I need to replace NaN in one column with value for other col
I've seen this come up before. You want to use np.where. data['Grade'] = np.where(data['Grade'].isna(),data['Score'],data['Grade']) here's an example that sets null values in grade to values in score, and if it's not null, leaves the current value. More on reddit.com
๐ŸŒ r/learnpython
10
1
July 15, 2021
[Pandas] Fill empty cells in column with value of other columns
Hello! Pandas is great. I hope you've got an interactive notebook, like Jupyter, as it will make it easier to play around with your dataframes. hc['ID'].fillna(hc['First Name']+hc['Last Name'], inplace=True) seemed to work for me. No if statement needed. More on reddit.com
๐ŸŒ r/learnpython
1
5
April 29, 2016
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third party โ€บ pandas โ€บ dataframe โ€บ replace()
Python Pandas DataFrame replace() - Replace Values
December 27, 2024 - Apply replace() to substitute a specific value. ... This code snippet substitutes the value 1 in column 'A' with 99. The rest of the DataFrame remains unchanged. Prepare a DataFrame with several integers.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ pandas
pandas: Replace values in DataFrame and Series with replace() | note.nkmk.me
January 17, 2024 - In pandas, the replace() method allows you to replace values in DataFrame and Series. It is also possible to replace parts of strings using regular expressions (regex). pandas.DataFrame.replace โ€” pan ...
๐ŸŒ
Data to Fish
datatofish.com โ€บ replace-values-pandas-dataframe
How to Replace Values in a pandas DataFrame
# replace one specific value in a column df['column_a'] = df['column_a'].replace("x", "y") # replace multiple values (x, y) with one value (z) in a column df['column_a'] = df['column_a'].replace(["x", "y"], "z") # replace values (w, x) with other values (y, z) in a column df['column_a'] = df['column_a'].replace(["w", "x"], ["y", "z"]) # replace one specific value in the entire df df = df.replace("x", "y")
๐ŸŒ
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 - In this post, you learned how to use the Pandas replace method to, well, replace values in a Pandas DataFrame. The .replace() method is extremely powerful and lets you replace values across a single column, multiple columns, and an entire DataFrame.
Find elsewhere
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ how to replace values in pandas
How to Replace Values in Pandas | Towards Data Science
January 16, 2025 - If you explore data regularly, probably you've faced more than once the need to replace some values, create some sort of categorization or simply replace a value that you needed to be shown some other way. There are a few ways to do that, as normally is the case for Python. Some are better than others in terms of coding. As I intend to create a short post, I'll go straight to the code snippets. The dataset I will use for the examples is the taxis from seaborn. ... Taxis dataset. Image by the author. Pandas replace() is a great method and it will let you do the trick quite fast.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ pandas โ€บ ref_df_replace.asp
Pandas DataFrame replace() Method
Cleaning Data Cleaning Empty Cells ... newdf = df.replace(50, 60) Try it Yourself ยป ยท The replace() method replaces the specified value with another specified value....
๐ŸŒ
Favtutor
favtutor.com โ€บ articles โ€บ pandas-replace-column-values
Pandas DataFrame: Replace Column Values (with code)
December 15, 2023 - The Pandas library provides the .replace() method in Python to replace columns in a DataFrame. The .replace() method is a versatile way to replace values in a Pandas DataFrame.
๐ŸŒ
Easy Tweaks
easytweaks.com โ€บ find-replace-values-pandas-dataframes
How to find and replace values in Pandas DataFrames and ...
September 10, 2022 - Master meetings, chats, channels and online collaboration ยท Go beyond the basics in Word, Excel, PowerPoint and Outlook
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas dataframe replace() โ€“ by examples
Pandas DataFrame replace() - by Examples - Spark By {Examples}
October 10, 2024 - pandas.DataFrame.replace() function is used to replace values in columns (one value with another value on all columns). It is a powerful tool for data
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ data analysis โ€บ python-pandas-dataframe-replace
Python | Pandas dataframe.replace() - GeeksforGeeks
Replacing more than one value at a time. Using python list as an argument We are going to replace team "Boston Celtics" and "Texas" with "Omega Warrior" in the 'df' Dataframe. ... # importing pandas as pd import pandas as pd # Making data frame from the csv file df = pd.read_csv("nba.csv") # this will replace "Boston Celtics" and "Texas" with "Omega Warrior" df.replace(to_replace=["Boston Celtics", "Texas"], value="Omega Warrior")
Published: July 11, 2025
๐ŸŒ
Quora
quora.com โ€บ How-do-you-change-the-value-of-a-cell-in-Pandas-DataFrame
How to change the value of a cell in Pandas DataFrame - Quora
Answer (1 of 2): Pandas Dataframe is a two-dimensional array which allows you to store data in rows and columns format. It is extensively used using python for data manipulation activities. During data manipulation activities, you may need to set the value of a cell in a pandas dataframe. You ca...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ pandas-dataframe-replace-values-in-column-based-on-condition
Pandas DataFrame - Replace values in column based on condition
To replace values in column based on condition in a Pandas DataFrame, you can use DataFrame.loc property, or numpy.where(), or DataFrame.where().
๐ŸŒ
Kanoki
kanoki.org โ€บ 2019 โ€บ 07 โ€บ 17 โ€บ pandas-how-to-replace-values-based-on-conditions
Pandas How to replace values based on Conditions | kanoki
July 17, 2019 - Using these methods either you can replace a single cell or all the values of a row and column in a dataframe based on conditions . ... import pandas as pd import numpy as np df = pd.DataFrame({'Date' : ['11/8/2011', '11/9/2011', '11/10/2011', '11/11/2011', '11/12/2011'], 'Event' : ['Dance', 'Painting', 'Dance', 'Dance', 'Painting']}) df
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ pandas: replace single cell in data frame: variable assignment not in place? what can i do?
r/learnpython on Reddit: Pandas: replace single cell in data frame: variable assignment not in place? what can I do?
December 2, 2022 -

Hi there,

I have a titanic data set with unclean entries. I want to change the sex from each entry to 'm' for male and 'f' for female.

The input looks like this:

107,1,3,"Salkjelsvik, Miss. Anna Kristine",1,21,0,0,343120,7.65,,S
108,1,3,"Moss, Mr. Albert Johan",-1,,0,0,312991,7.775,,S
109,0,3,"Rekic, Mr. Tido",-1,38,0,0,349249,7.8958,,S
110,1,3,"Moran, Miss. Bertha",1,,1,0,371110,24.15,,Q
111,0,1,"Porter, Mr. Walter Chamberlain",-1,47,0,0,110465,52,C110,S
112,0,3,"Zabour, Miss. Hileni",female,14.5,1,0,2665,14.4542,,C
113,0,3,"Barton, Mr. David John",male,22,0,0,324669,8.05,,S

I tried

import pandas as pd

data = pd.read_csv('titanicClean.csv')

for i in range(len(data)):

    if data.iloc[i].Sex == 'f' or data.iloc[i].Sex == 'm':
        continue
    else:
        if 'f' in data.iloc[i].Sex or data.iloc[i].Sex == '1':
            data.iloc[i].Sex = 'f'
        else:
            data.iloc[i].Sex = 'm'

yet either it doesn't change the values or it doesn't do it inplace. I also tried data.iloc[i].Sex.replace(data.iloc[i].Sex, 'f', inplace = True) yet here it complains that the function doesn't accept keywords and data[data.Sex == data.iloc[i].Sex] = 'm' which has the same problem as above.

Is there a way to replace single cell values in pandas by variable assignment or do I need a special function for this?

๐ŸŒ
pandas
pandas.pydata.org โ€บ pandas-docs โ€บ dev โ€บ reference โ€บ api โ€บ pandas.DataFrame.replace.html
pandas.DataFrame.replace โ€” pandas 3.1.0.dev0 documentation
For a DataFrame a dict can specify that different values should be replaced in different columns. For example, {'a': 1, 'b': 'z'} looks for the value 1 in column โ€˜aโ€™ and the value โ€˜zโ€™ in column โ€˜bโ€™ and replaces these values with whatever is specified in value.