Ad

Replacing The Value In A Column Based On A Single Vectorized Condition

I tried the following code with multiple conditions and worked perfectly, somehow whatever I am trying with a single condition it won't work. It will simply return the values "1" & "SD" for every row no matter if the value of Col_A is True. Please let me know any suggestions, the weird part is that I am not getting any error whatsoever.

condition = [
    df1['Col_A'] == 'TRUE'
]

result1 = ['1']
result2 = ['SD']

df1['Col_B'] = np.select(condition, result1, 'NA')
df1['Col_C'] = np.select(condition, result2, 'NA')
df1```

Input

        Col_A    Col_B     Col_C
    0   TRUE       NA         NA     
    1   FALSE      1001       Valid  
    2   TRUE       NA         NA    
    3   TRUE       NA         NA

Output

        Col_A    Col_B     Col_C
    0   TRUE       NA         NA     
    1   FALSE      NA         NA  
    2   TRUE       NA         NA    
    3   TRUE       NA         NA
  
Desired Output

        Col_A    Col_B     Col_C
    0   TRUE       1         SD     
    1   FALSE      1001      Valid  
    2   TRUE       1         SD    
    3   TRUE       1         SD
Ad

Answer

You could use a simple loc assignment with a condition:

string "TRUE":

df.loc[df['Col_A'].eq('TRUE'), ['Col_B', 'Col_C']] = [1, 'SD']

boolean True:

df.loc[df['Col_A'], ['Col_B', 'Col_C']] = [1, 'SD']

output:

   Col_A   Col_B  Col_C
0   TRUE     1.0     SD
1  FALSE  1001.0  Valid
2   TRUE     1.0     SD
3   TRUE     1.0     SD
Ad
source: stackoverflow.com
Ad