Adding column3
with:
df['column3'] = np.where((df['column2']) == 1, '1', 0)
to the 2 columns existing table don't feed the 1's
and the result is column3
full with 0's
column1 column2 column3
0 1 0
1 0 0
0 0 0
0 1 0
1 0 0
CodePudding user response:
FTR, the problem seems to be that the column has string values, meaning no value will be equal to 1
. Use "1"
instead:
df['column3'] = np.where(df['column2'] == "1", "1", 0)
CodePudding user response:
So the following code is working for me. It is essentially the same as your code.
import pandas as pd
import numpy as np
d = {'column1': [0, 1, 0, 0, 1], 'column2': [1, 0, 0, 1, 1], 'column3': [0, 0, 0, 0, 0]}
df = pd.DataFrame(data=d)
df['column3'] = np.where((df['column2']) == 1 , '1', 0)
```
The output is:
[enter image description here][1]
[1]: https://i.stack.imgur.com/sgodK.png
So please check the datatype of "column2", if it is a string the condition may be false.