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 ...
🌐
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 - See example that follows, where I change the values that are lower than 18 hours (aka 6pm) to 'day'. ... [OUT]:0 20 1 day 2 day 3 day 4 day ... 6428 day 6429 day 6430 22 6431 day Name: pickup_hour, Length: 6433, dtype: object · The select() function is pretty similar to the Pandas replace() method.
🌐
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’ll learn how to use the Pandas .replace() method to replace data in your DataFrame. The Pandas DataFrame.replace() method can be used to replace a string, values, and even regular expressions (regex) in your DataFrame.
🌐
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")
Find elsewhere
🌐
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....
🌐
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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › data analysis › python-pandas-dataframe-replace
Python | Pandas dataframe.replace() - GeeksforGeeks
Pandas dataframe.replace() function is used to replace a string, regex, list, dictionary, series, number, etc. from a Pandas Dataframe in Python. Every instance of the provided value is replaced after a thorough search of the full DataFrame.
Published: July 11, 2025
🌐
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
🌐
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
In the following program, we will replace those values in the column 'a' that satisfy the condition that the value is less than zero. import pandas as pd df = pd.DataFrame([ [-10, -9, 8], [6, 2, -4], [-8, 5, 1]], columns=['a', 'b', 'c']) df.loc[(df.a < 0), 'a'] = 0 print(df)
🌐
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 - Replace all the Dance in Column Event with Hip-Hop · df.loc[(df.Event == 'Dance'),'Event']='Hip-Hop' df ... df = pd.DataFrame([[1.4, 8], [1.2, 5], [0.3, 10]], index=['China', 'India', 'USA'], columns=['Population(B)', 'Economy']) ... DataFrames are a powerful tool for working with data in Python, and Pandas provides a number of ways to count duplicate rows in a DataFrame. In this article... ... If you’re encountering a “value error” while merging Pandas data frames, this article has got you covered.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-replace-values-in-column-based-on-condition-in-pandas
How to Replace Values in Column Based on Condition in Pandas? - GeeksforGeeks
November 15, 2024 - Code: Python3 # import pandas library ... can be easily performed using a single line DataFrame.fillna() and DataFrame.replace() method....
🌐
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.