0

I don't know how to right properly the following idea: I have a dataframe that has two columns, and many many rows. I want to create a new column based on the data in these two columns, such that if there's 1 in one of them the value will be 1, otherwise 0. Something like that:

if (df['col1']==1 | df['col2']==1):
   df['newCol']=1
else:
   df['newCol']=0

I tried to use .loc function in different ways but i get different errors, so either I'm not using it correctly, or this is not the right solution...

Would appreciate your help. Thanks!

  • Possible duplicate of this post: [pandas-conditional-creation-of-a-series-dataframe-column](https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column) – Alexandre B. Jun 10 '19 at 11:31

2 Answers2

1

Simply use np.where or np.select

df['newCol'] = np.where((df['col1']==1 | df['col2']==1), 1, 0)

OR

df['newCol'] = np.select([cond1, cond2, cond3], [choice1, choice2, choice3], default=def_value)

When a particular condition is true replace with the corresponding choice(np.select).

Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
0

one way to solve this using .loc,

df.loc[(df['col1'] == 1 | df['col2']==1) ,'newCol'] = 1
df['newCol'].fillna(0,inplace=True)

incase if you want newcol as string use,

df.loc[(df['col1'] == 1 | df['col2']==1) ,'newCol'] = '1'
df['newCol'].fillna('0',inplace=True)

or

df['newCol']=df['newCol'].astype(str)
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111