You need to select that column:
In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df
Out[41]:
Team First Season Total Games
0 Dallas Cowboys 1960 894
1 Chicago Bears 1920 1357
2 Green Bay Packers 1921 1339
3 Miami Dolphins 1966 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 1950 1003
So the syntax here is:
df.loc[<mask>(here mask is generating the labels to index) , <optional column(s)> ]
You can check the docs and also the 10 minutes to pandas which shows the semantics
EDIT
If you want to generate a boolean indicator then you can just use the boolean condition to generate a boolean Series and cast the dtype to int this will convert True and False to 1 and 0 respectively:
In [43]:
df['First Season'] = (df['First Season'] > 1990).astype(int)
df
Out[43]:
Team First Season Total Games
0 Dallas Cowboys 0 894
1 Chicago Bears 0 1357
2 Green Bay Packers 0 1339
3 Miami Dolphins 0 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 0 1003
Answer from EdChum on Stack OverflowSpark By {Examples}
sparkbyexamples.com โบ home โบ pandas โบ pandas replace values based on condition
Pandas Replace Values based on Condition - Spark By {Examples}
June 18, 2025 - You can replace all values or selected values in a column of pandas DataFrame based on condition by using DataFrame.loc[], np.where() and DataFrame.mask()
python - Pandas DataFrame: replace all values in a column, based on condition - Stack Overflow
I have a simple DataFrame like the following: Team First Season Total Games 0 Dallas Cowboys 1960 894 1 Chicago Bears 1920 1357 2 Green Bay Packers 1921 1339 3 Miami Dolphins 1966 792 4 Baltimore R... More on stackoverflow.com
python - How to replace all value in all columns in a Pandas dataframe with condition - Stack Overflow
1 How can all values of certain included or excluded columns of a DataFrame be impuded based on a condition? 295 Pandas DataFrame: replace all values in a column, based on condition More on stackoverflow.com
how to update a pandas dataframe column value, when a specific string appears in another column?
It's not something you'd really use .apply for. You would use boolean indexing, e.g. df['A'].str.contains('foo') would give you a Series of True/False values. You can then use .loc to set column(s) to a particular value for the True rows: df.loc[df['A'].str.contains('foo'), 'B'] = 'bar' More on reddit.com
A most efficient way of using conditional replacement in pandas
You can assign multiple columns to the same value at the same time. >>> df foo bar baz 0 1 4 7 1 2 5 8 2 1 6 9 >>> df.loc[ df['foo'] == 1, ['baz', 'bar']] = '-' >>> df foo bar baz 0 1 - - 1 2 5 8 2 1 - - It's .loc[rows, columns] = value >>> rows = df['foo'] == 1 >>> columns = 'bar', 'baz' >>> df.loc[rows, columns] = '-' >>> df foo bar baz 0 1 - - 1 2 5 8 2 1 - - More on reddit.com
13:45
Clean Your Data FAST with Pandas replace() - YouTube
06:59
Replace Values of pandas DataFrame in Python (Example) | Substitute ...
06:24
Replacing a value in a Column | Python Panda Tutorial - YouTube
06 Replacing Column Data in Pandas With Alternative Values
03:18
How to Replace the Column Values in Pandas Dataframe - YouTube
11:32
How to Replace Values of Dataframes | Replace, Where, Mask, Update ...
Top answer 1 of 10
460
You need to select that column:
In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df
Out[41]:
Team First Season Total Games
0 Dallas Cowboys 1960 894
1 Chicago Bears 1920 1357
2 Green Bay Packers 1921 1339
3 Miami Dolphins 1966 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 1950 1003
So the syntax here is:
df.loc[<mask>(here mask is generating the labels to index) , <optional column(s)> ]
You can check the docs and also the 10 minutes to pandas which shows the semantics
EDIT
If you want to generate a boolean indicator then you can just use the boolean condition to generate a boolean Series and cast the dtype to int this will convert True and False to 1 and 0 respectively:
In [43]:
df['First Season'] = (df['First Season'] > 1990).astype(int)
df
Out[43]:
Team First Season Total Games
0 Dallas Cowboys 0 894
1 Chicago Bears 0 1357
2 Green Bay Packers 0 1339
3 Miami Dolphins 0 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 0 1003
2 of 10
95
A bit late to the party but still - I prefer using numpy where:
import numpy as np
df['First Season'] = np.where(df['First Season'] > 1990, 1, df['First Season'])
Pandas
pandas.pydata.org โบ docs โบ reference โบ api โบ pandas.DataFrame.replace.html
pandas.DataFrame.replace โ pandas 3.0.5 documentation
Replace values based on boolean condition. ... Apply a function to a Dataframe elementwise. ... Map values of Series according to an input mapping or function. ... Simple string replacement. ... Regex substitution is performed under the hood with re.sub. The rules for substitution for re.sub are the same. Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched.
IncludeHelp
includehelp.com โบ python โบ how-to-replace-all-values-in-a-column-based-on-condition.aspx
Pandas: How to replace all values in a column, based on condition?
September 21, 2023 - # Importing pandas package import pandas as pd # creating a dictionary of student marks d = { "Players":['Sachin','Ganguly','Dravid','Yuvraj','Dhoni','Kohli'], "Format":['ODI','ODI','ODI','ODI','ODI','ODI'], "Runs":[15921,7212,13228,1900,4876,8043] } # Now we will create DataFrame df = pd.DataFrame(d) # Viewing the DataFrame print("DataFrame:\n",df,"\n\n") # Replacing thr values of column Format df.loc[(df.Format == 'ODI' ), 'Format'] = 'TEST' # Display modified DataFrame print("Modified DataFrame:\n",df)
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)
Scaler
scaler.com โบ home โบ topics โบ pandas โบ conditional changes in pandas dataframe
Conditional Changes in Pandas Dataframe - Scaler Topics
May 4, 2023 - With the help of pandas DataFrame.loc[] property, values of the selected columns based on the provided condition of the pandas DataFrame can be replaced. By using a label or boolean array, the loc[] allows you to access a collection of rows and columns.
Skytowner
skytowner.com โบ explore โบ replacing_values_in_a_dataframe_in_pandas
Replacing values in a DataFrame in Pandas
Replacing a value in entire ... 100+ top-tier guides Start your free 7-days trial now! To replace values in a Pandas DataFrame, use the DataFrame's replace(~) method....
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.
Arab Psychology
scales.arabpsychology.com โบ psychological scales โบ how to use pandas to replace values in column based on condition
How To Use Pandas To Replace Values In Column Based On Condition
November 15, 2023 - Pandas provides a method called .loc to replace values in a column based on condition. It takes a list of conditions as the first argument and a list of values to replace the conditions with as the second argument. The syntax is df.loc[conditions, โcolumnโ] = new_values.
RS Blog
reneshbedre.com โบ blog โบ replace-column-values-based-on-condition-pandas.html
How to replace column values in pandas DataFrame based on column conditions
December 18, 2022 - This method is more suitable if you want to update the large number of values based on condition in a column. ... condition: conditional expression true_value: Old value will be replaced with this true value if the condition is True false_value: Old value will be replaced with this value if the condition is False ยท import pandas as pd # create a random dataframe df = pd.DataFrame({'name':['Adams', 'Jones', 'Frank', 'Smith', 'Davis'], 'age':[25, 30, 28, 35, 22], 'weight':[74, 90, 85, 65, 92]}) # output name age weight 0 Adams 25 74 1 Jones 30 90 2 Frank 28 85 3 Smith 35 65 4 Davis 22 92 # replace the weight value with 92 if it is 90 df['weight'].mask(df['weight'] == 90, 98, inplace=True) # output name age weight 0 Adams 25 74 1 Jones 30 98 2 Frank 28 85 3 Smith 35 65 4 Davis 22 92