Overwrite value in cell based on condition, without overwriting some of the existing values

Question:

The goal would be to overwrite a value depending on other conditions, but if a value is certain string don’t overwrite it. I’ve just began to code and am not sure how to solve this, or if my approach is the most efficient.

This is what I’m currently trying:

for cell_value in df['col1']:
    if cell_value == 'a':
        continue 
'''
with this part I'm trying to get the code not to alter these existing values
'''
    for cell_value in df['col1']:
        if cell_value in df.loc((df['col2'] == 'red') & ((df['number']) == 0) & 
        ((df['col_4'].str.contains("color")))):
            cell_value = 'This is red'
'''            
With this part I'm attempting for the code to change the cell_value in 'col1' to 
'This is red' while not changing those values in 'col1' that equal 'a' 
'''            

I’m not even sure if I’m on the right track, or if this can be done with mask(), which I do not completely get.

The current error I’m getting is:

TypeError: unhashable type: ‘Series’

Any help would be appreciated.

Asked By: Jose Daniel Rojas

||

Answers:

i think you don’t need to a loop. Can be done with np.where(). This will work if i understood the question correctly.

import numpy as np
df['col1']=np.where(((df['col1']!='a') & (df['col2'] == 'red') & ((df['number']) == 0) & ((df['col_4'].str.contains("color")))),'This is red',df['col1'])

Answered By: Clegane
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.