0

I'm trying to set a column based on the value of other columns in my dataframe, but i'm having a hard time with the syntax of this. I can best describe this with an example:

Say you have a dataframe: the columns "Computer", "IP", "IP2" "Signal", "Connected"

data = {'Computer':['cp1', 'cp2'], 'IP1':[51.20, 51.21], IP2:[52.20, 52.21], 'Signal':[IN, OUT]}
df = pd.DataFrame(data)
df[Connected]=np.nan

Here's what I've tried:

for i in df['Signal']:
    
    if i =='IN':
        df['Connected']= df['IP2']
    else: df['Connected'] =df[IP1]

But this doesn't give me the correct output.

What I would like as an output is for every instance of 'IN' Connected takes the value of IP2 And for every instance of 'OUT' it takes the value of IP1

I hope this makes sense. Thank you

1 Answers1

0

Use mask with the right condition

df['Connected'] = df['IP1'].mask(df['Signal'] == 'IN', df['IP2'])

df
Out[20]: 
  Computer    IP1    IP2 Signal  Connected
0      cp1  51.20  52.20     IN      52.20
1      cp2  51.21  52.21    OUT      51.21
Onyambu
  • 67,392
  • 3
  • 24
  • 53