-1

I have the following dataframe called df1.

   Country  ticker  tickeralt finalticker
0  US       AAPL    AAPL.O
1  US       BABA    BABA.O
2  US       AMD     AMD.O    
3  US       MSFT    MSFT.O
4  T        9984    9984.T
5  T        6752    6752.T
6  US       EXC     EXC.O
7  US       PANW    PANW.K

I want to do the following, if Country == 'US', then I want to set finalticker to whats in the ticker column for that row, and if its any other country but 'US', I want the finalticker for that row to be tickeralt, so it should look like this.

   Country  ticker  tickeralt finalticker
0  US       AAPL    AAPL.O    AAPL    
1  US       BABA    BABA.O    BABA    
2  US       AMD     AMD.O     AMD     
3  US       MSFT    MSFT.O    MSFT
4  T        9984    9984.T    9984.T
5  T        6752    6752.T    6752.T
6  US       EXC     EXC.O     EXC     
7  US       PANW    PANW.K    PANW

How do I do this? I have no idea where to start.

anarchy
  • 3,709
  • 2
  • 16
  • 48
  • 1
    Use `df['finalticker'] = np.where(df['Country'].eq('US'), df['ticker'], df['tickeralt'])` – Space Impact May 05 '20 at 22:13
  • Does this answer your question? [Update row values where certain condition is met in pandas](https://stackoverflow.com/questions/36909977/update-row-values-where-certain-condition-is-met-in-pandas) – Space Impact May 05 '20 at 22:14
  • yup you should post the answer so i can accept it – anarchy May 05 '20 at 22:56

1 Answers1

0
df1.finalticker = [df.ticker else df.tickeralt if df.Counter == "US" for df in df1]
Gwang-Jin Kim
  • 9,303
  • 17
  • 30
  • While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – borchvm May 06 '20 at 08:36