So looking at this question a couple years later I see the error, to coerce the returned result so it assigns correctly you need to access the scalar values and use these to assign so they align as desired:

In [22]:
df.loc[df['A'] == 1, ['A', 'B']] = df['C'].values[0] + 10,df['C'].values[0] + 11
df

Out[22]:
    A   B  C
0  11  12  1
1   2   2  2
2   3   3  3
Answer from EdChum on Stack Overflow
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-update-column-values-in-pandas-based-on-criteria-from-another-column
How to Update Column Values in Pandas Based on Criteria From Another Column | Saturn Cloud Blog
January 18, 2024 - We do this by applying a lambda function to each row of the DataFrame using the apply function. The lambda function checks if the value in column1 is greater than 10, and if so, returns the value in column1.
Discussions

data mining - Pandas change value of a column based another column condition - Data Science Stack Exchange
I have values in column1, I have columns in column2. What I want to achieve: Condition: where column2 == 2 leave to be 2 if column1 90. Here is what i did s... More on datascience.stackexchange.com
๐ŸŒ datascience.stackexchange.com
python - pandas : update value if condition in 3 columns are met - Stack Overflow
I have a dataframe df like this: A B C D 1 blue red square NaN 2 orange yellow circle NaN 3 black grey circle NaN and I want to up... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Pandas update multiple columns at once - Stack Overflow
I'm trying to update a couple fields at once - I have two data sources and I'm trying to reconcile them. I know I could do some ugly merging and then delete columns, but was expecting this code be... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Update row values where certain condition is met in pandas - Stack Overflow
Say I have the following dataframe: What is the most efficient way to update the values of the columns feat and another_feat where the stream is number 2? Is this it? for index, row in df.iterrows... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ update-rows-and-columns-python-pandas
How to Update Rows and Columns Using Python Pandas | DigitalOcean
August 4, 2022 - Based on the output, we have 2 fruits whose price is more than 60. Letโ€™s quote those fruits as expensive in the data. #Updating data.loc[updated, 'Price'] = 'Expensive' data ยท Trust me, you are awesome :). You did it in an amazing way and with perfection. In this whole tutorial, I have never used more than 2 lines of code. The best suggestion I can give is, to try to learn pandas as much as possible.
๐ŸŒ
Allthesnippets
allthesnippets.com โ€บ browse โ€บ pandas โ€บ df_selection.html
Pandas snippets and cheat sheet for data selection with .loc & .iloc
Subset pandas DataFrame by index/labels and columns (.loc) or by integer position (.iloc) on index (rows) or columns. Update column values based on rows condition.
Top answer
1 of 4
13

What I want to achieve: Condition: where column2 == 2 leave to be 2 if column1 < 30 elsif change to 3 if column1 > 90

This can be simplified into where (column2 == 2 and column1 > 90) set column2 to 3. The column1 < 30 part is redundant, since the value of column2 is only going to change from 2 to 3 if column1 > 90.

In the code that you provide, you are using pandas function replace, which operates on the entire Series, as stated in the reference:

Values of the Series are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value.

This means that for each iteration of for x in filter1 your code performs global replacement, which is not what you want to do - you want to update the specific row of column2 that corresponds to x from column1 (which you are iterating over).

the problem is 2 does not change to 3 where column1 > 90

This is truly strange. I would expect the code you provided to have changed every instance of 2 in column2 to 3 as soon as it encountered an x >= 30, as dictated by your code conditional statement (the execution of the else branch). This discrepancy may stem from the fact that you are assigning to column2 the result of global replacement performed on the column Output (the contents of which are unknown). In any case, if you want your program to do something under a specific condition, such as x > 90, it should be explicitly stated in the code. You should also note that the statement data['column2'] = data['column2'].replace([2], [2]) achieves nothing, since 2 is being replaced with 2 and the same column is both the source and the destination.

What you could use to solve this particular task is a boolean mask (or the query method). Both are explained in an excellent manner in this question.

Using a boolean mask would be the easiest approach in your case:

mask = (data['column2'] == 2) & (data['column1'] > 90)
data['column2'][mask] = 3

The first line builds a Series of booleans (True/False) that indicate whether the supplied condition is satisfied. The second line assigns the value 3 to those rows of column2 where the mask is True.

2 of 4
12

I've had success approaching this in a slightly different way.

import numpy as np

data['column2'] = np.where((data['column1'] < 30)
                           & (data['column2'] ==2), #Identifies the case to apply to
                           data['column2'],      #This is the value that is inserted
                           data['column2'])      #This is the column that is affected
data['column2'] = np.where((data['column1'] > 90)
                           & (data['column2'] ==2), #For rows with column1 > 90
                           data['column3'],      #We place column3 values
                           data['column2'])      #In column two

This is a little wordier than a loop, but I've found it to be the most intuitive way to do this sort of data manipulation with pandas.

๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ pandas-update-value-if-condition-in-3-columns-are-met.aspx
Python Pandas - Update value if condition in 3 columns are met
September 26, 2023 - To perform various operations using the The pandas.DataFrame.loc property, we need to pass the required condition of rows and columns to get the filtered data. ... # Importing pandas package import pandas as pd # Importing numpy package import numpy as np # Creating a dictionary d = { ...
๐ŸŒ
Medium
macxima.medium.com โ€บ python-how-to-update-multiple-columns-at-once-in-dataframe-ea801f6a70a8
Python โ€” How to update multiple columns at once in dataframe? | by Ryan Arjun | Medium
May 25, 2024 - We understand, we can add a column to a dataframe and update its values to the values returned from a function or other dataframe columnโ€™s values as given below - # pandas library for data manipulation in python import pandas as pd# create a dataframe with number values df = pd.DataFrame({'Num':[5,10,15,17,22,25,28,32,36,40,50,]})#display values from dataframe df#create square() function to return single value #passing variable is x #return single valuedef square(x): return x*x#Add new column and update value in it df['Square of Num'] = [square(i) for i in df['Num']]#display values from dataframe df
Find elsewhere
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas replace values based on condition
Pandas Replace Values based on Condition - Spark By {Examples}
June 18, 2025 - In this article, I will explain how to change all values in columns based on the condition in Pandas DataFrame with different methods of simple examples.
๐ŸŒ
Easy Tweaks
easytweaks.com โ€บ home โ€บ how to update row values based on condition in pandas dataframes?
How to update row values based on condition in pandas DataFrames?
December 26, 2022 - We can use the fillna() function to replace values in the team column. ... Next we will go ahead and adjust the value of specific row cell based on a condition. # define condition rows = (revenue.indirect_sales > 150) # update dataframe revenue.loc ...
Top answer
1 of 3
44

you want to replace

print df.loc[df['Col1'].isnull(),['Col1','Col2', 'Col3']]

  Col1 Col2 Col3
2  NaN  NaN  NaN
3  NaN  NaN  NaN

With:

replace_with_this = df.loc[df['Col1'].isnull(),['col1_v2','col2_v2', 'col3_v2']]
print replace_with_this

  col1_v2 col2_v2 col3_v2
2       a       b       d
3       d       e       f

Seems reasonable. However, when you do the assignment, you need to account for index alignment, which includes columns.

So, this should work:

df.loc[df['Col1'].isnull(),['Col1','Col2', 'Col3']] = replace_with_this.values

print df

  Col1 Col2 Col3 col1_v2 col2_v2 col3_v2
0    A    B    C     NaN     NaN     NaN
1    D    E    F     NaN     NaN     NaN
2    a    b    d       a       b       d
3    d    e    f       d       e       f

I accounted for columns by using .values at the end. This stripped the column information from the replace_with_this dataframe and just used the values in the appropriate positions.

2 of 3
2

In the "take the hill" spirit, I offer the below solution which yields the requested result.

I realize this is not exactly what you are after as I am not slicing the df (in the reasonable - but non functional - way in which you propose).

#Does not work when indexing on np.nan, so I fill with some arbitrary value. 
df = df.fillna('AAA')

#mask to determine which rows to update
mask = df['Col1'] == 'AAA'

#dict with key value pairs for columns to be updated
mp = {'Col1':'col1_v2','Col2':'col2_v2','Col3':'col3_v2'}

#update
for k in mp: 
     df.loc[mask,k] = df[mp.get(k)]

#swap back np.nans for the arbitrary values
df = df.replace('AAA',np.nan)

Output:

Col1    Col2    Col3    col1_v2     col2_v2     col3_v2
A       B       C       NaN         NaN         NaN
D       E       F       NaN         NaN         NaN
a       b       d       a           b           d
d       e       f       d           e           f

The error I get if I do not replace nans is below. I'm going to research exactly where that error stems from.

ValueError: array is not broadcastable to correct shape
Top answer
1 of 3
358

I think you can use loc if you need update two columns to same value:

df1.loc[df1['stream'] == 2, ['feat','another_feat']] = 'aaaa'
print df1
   stream        feat another_feat
a       1  some_value   some_value
b       2        aaaa         aaaa
c       2        aaaa         aaaa
d       3  some_value   some_value

If you need update separate, one option is use:

df1.loc[df1['stream'] == 2, 'feat'] = 10
print df1
   stream        feat another_feat
a       1  some_value   some_value
b       2          10   some_value
c       2          10   some_value
d       3  some_value   some_value

Another common option is use numpy.where:

df1['feat'] = np.where(df1['stream'] == 2, 10,20)
print df1
   stream  feat another_feat
a       1    20   some_value
b       2    10   some_value
c       2    10   some_value
d       3    20   some_value

EDIT: If you need divide all columns without stream where condition is True, use:

print df1
   stream  feat  another_feat
a       1     4             5
b       2     4             5
c       2     2             9
d       3     1             7

#filter columns all without stream
cols = [col for col in df1.columns if col != 'stream']
print cols
['feat', 'another_feat']

df1.loc[df1['stream'] == 2, cols ] = df1 / 2
print df1
   stream  feat  another_feat
a       1   4.0           5.0
b       2   2.0           2.5
c       2   1.0           4.5
d       3   1.0           7.0

If working with multiple conditions is possible use multiple numpy.where or numpy.select:

df0 = pd.DataFrame({'Col':[5,0,-6]})

df0['New Col1'] = np.where((df0['Col'] > 0), 'Increasing', 
                          np.where((df0['Col'] < 0), 'Decreasing', 'No Change'))

df0['New Col2'] = np.select([df0['Col'] > 0, df0['Col'] < 0],
                            ['Increasing',  'Decreasing'], 
                            default='No Change')

print (df0)
   Col    New Col1    New Col2
0    5  Increasing  Increasing
1    0   No Change   No Change
2   -6  Decreasing  Decreasing
2 of 3
5

You can do the same with .ix, like this:

In [1]: df = pd.DataFrame(np.random.randn(5,4), columns=list('abcd'))

In [2]: df
Out[2]: 
          a         b         c         d
0 -0.323772  0.839542  0.173414 -1.341793
1 -1.001287  0.676910  0.465536  0.229544
2  0.963484 -0.905302 -0.435821  1.934512
3  0.266113 -0.034305 -0.110272 -0.720599
4 -0.522134 -0.913792  1.862832  0.314315

In [3]: df.ix[df.a>0, ['b','c']] = 0

In [4]: df
Out[4]: 
          a         b         c         d
0 -0.323772  0.839542  0.173414 -1.341793
1 -1.001287  0.676910  0.465536  0.229544
2  0.963484  0.000000  0.000000  1.934512
3  0.266113  0.000000  0.000000 -0.720599
4 -0.522134 -0.913792  1.862832  0.314315

EDIT

After the extra information, the following will return all columns - where some condition is met - with halved values:

>> condition = df.a > 0
>> df[condition][[i for i in df.columns.values if i not in ['a']]].apply(lambda x: x/2)
๐ŸŒ
Skytowner
skytowner.com โ€บ explore โ€บ conditionally_updating_values_of_a_dataframe_in_pandas
Conditionally updating values of a DataFrame in Pandas
To conditionally update values in a Pandas DataFrame, create a boolean mask and then pass it into loc, and finally perform assignment.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to update a pandas dataframe column value, when a specific string appears in another column?
r/learnpython on Reddit: how to update a pandas dataframe column value, when a specific string appears in another column?
June 24, 2024 -

So, i've figured out how to use the pandas apply method to update/change the values of a column, row-wise based on multiple comparisons like this:

# for each row, if the value of both 'columns to check' are 'SOME STRING', change to 'NEW STRING
# otherwise leave it as is
my_df ['column_to_change'] = df.apply(lambda row: 'NEW STRING' if row['column_to_check_1'] and row['column_to_check_2'] == 'SOME STRING' else row['column_to_change'], axis=1)

Now, I can't figure out how to expand that beyond simple comparison operators. The specific example I'm trying to solve is:

" for each row, if the string value in COLUMN A contains 'foo', change the value in COLUMN B to 'bar', otherwise leave it as is"

I think this is all right, except the ##parts between the hashmarks##

my_df ['columb_b'] = df.apply(lambda row: 'bar' if ##column A contains 'foo'## else row['columb_b'], axis=1)
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ pandas_update_cell_value_based_on_condition.php
Pandas Update Cell Value Based on Condition (2024): Master Conditional Updates & Transform Your Data Like a Pro!
A: The most recommended and safest way to update cell values in pandas based on a condition is to use the .loc accessor: df.loc[row_condition, 'column_name'] = new_value. This prevents SettingWithCopyWarning and ensures you're modifying the intended DataFrame. Q: How can I update multiple columns in pandas based on a single condition?