3

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?

2 Answers 2

2

We can do sign from numpy

df['col3']=np.sign((df.col2-df.col1))+1
Sign up to request clarification or add additional context in comments.

1 Comment

Just great! Thank you!
1

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

1 Comment

np.select is a god way as well. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.