'set a column based on other columns in pandas [duplicate]
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | onyambu |
