Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set DataFrame column values conditionally without a loop in Python

I have the following Pandas DataFrame:

enter image description here

(My original DataFrame is a lot bigger than the one in this example.)

I need to add another column (col3) to this DataFrame and values of col3 will be set based on following conditions:

  • If col1 > col2, value of col3 will be set to 0 on that row.
  • If col1 == col2, value of col3 will be set to 1 on that row.
  • If col1 < col2, value of col3 will be set to 2 on that row.

The above DataFrame will look as the following after this operation:

enter image description here

Is there a way to do this without looping through the DataFrame?

like image 545
edn Avatar asked Jun 15 '26 11:06

edn


2 Answers

We can do sign from numpy

df['col3']=np.sign((df.col2-df.col1))+1
like image 54
BENY Avatar answered Jun 18 '26 01:06

BENY


You can do np.select:

df['col3'] = np.select((df['col1'] > df['col2'], df['col1'] < df['col2']),
                       (0, 2), 1)

Or use np.sign:

df['col3'] = np.sign(df['col2']-df['col1']) + 1
like image 45
Quang Hoang Avatar answered Jun 18 '26 02:06

Quang Hoang