2

I have the following dataframe

Year          M  
1991-1990     10
1992-1993      9

What I am trying to so is a if statement: =IF(M>9,LEFT(Year),RIGHT(C2,4))*1

So basically if M if 10 choose the left value of the column year else choose the second value

I tried using np.where but I have no idea how to choose between two values in the same column.

Help?

1 Answer 1

1

You can do this:

In [448]:  df['val'] = np.where( df['M'].gt(9),\ 
     ...:                       df.Year.str.split('-').tolist()[0],\ 
     ...:                       df.Year.str.split('-').tolist()[1] )                                                                                                                                         


In [444]: df                                                                                                                                                                                                
Out[444]: 
        Year   M   val
0  1991-1990  10  1991
1  1992-1993   9  1993
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.