Issue
I would like to add a new column called XXX based on the last letter in the "indicator" column if:
indicator ends with an S -> Use the value from 'Vendor 5 Ask Price XXX'
indicator ends with an B -> Use the value from 'Vendor 5 Bid Price XXX'
so the new column would be : XXX : [100,nan,107,103]
df = {'indicator': ['45346B','24536S','34636S','657363B'],
'Vendor 5 Bid Price XXX' : [100,None,102,103],
'Vendor 5 Ask Price XXX' : [105,None,107,108]}
pd.DataFrame(df)
indicator Vendor 5 Bid Price XXX Vendor 5 Ask Price XXX
0 45346B 100.0 105.0
1 24536S NaN NaN
2 34636S 102.0 107.0
3 657363B 103.0 108.0
Solution
What about
df[‘XXX’] = df.apply(
lambda row: row[‘Vendor 5 Ask Price XXX’] if row[‘indicator’].ends with(‘S’) else row[‘Vendor 5 Bid Price XXX’],
axis=1
)
.apply(…, axis=1)
will apply the function to every row. The lambda function is just the implementation of the switch logic you mentioned and can be more complex if needed.
Answered By - Baobab
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.