1

    A   B   C
0   0   -2  1
1   1   -1  2
2   2   0   3
3   3   1   0
4   4   2   5

I want to get a new column such that it picks elements from B if B is positive (>0) otherwise picks elements from C

    A   B   C    D
0   0   -2  1    1
1   1   -1  2    2 
2   2   0   3    3
3   3   1   0    1  
4   4   2   5    5 
5
  • Can you clarify exactly what you want? Does it have to sum B and C (eg row 4 -> 2 + 5 is not 5), or just the maximum of both columns? Commented Mar 19, 2014 at 14:03
  • I want to get a new column such that it picks elements from B if B is positive (>0) otherwise picks elements from C Commented Mar 19, 2014 at 14:09
  • Also what should be done to get something like D(x) = B(x) +C(x) if B(x)>0 else C(x) Commented Mar 19, 2014 at 14:10
  • Your row 4 does not follow this. Commented Mar 19, 2014 at 14:14
  • Yeah That was a follow up question. May be I will creste a new question and post it back Commented Mar 19, 2014 at 14:35

1 Answer 1

2

Use a Boolean mask:

In [14]: df[df[['B', 'C']] > 0].max(1)
Out[14]: 
0    1
1    2
2    3
3    1
4    5
dtype: float64

In [15]: df['D'] = df[df[['B', 'C']] > 0].max(1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! any ideas how to do D(x) = B(x) +C(x) if B(x)>0 else C(x) efficiently

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.