Situation
I have a pandas dataframe dfwith a column sentiment_rating.
| index | sentiment_rating |
|---|---|
| 2022-03-20 | .3 |
| 2022-03-21 | -.4 |
| 2022-03-24 | -.7 |
| 2022-03-28 | .6 |
| 2022-03-31 | .2 |
Goal
I'm trying to create a new column status who's value will be either positive if the sentiment score is .5 or greater, negative if -.5 or less, or neutral if between -.5 and .5.
What I've tried
I've installed the pandas DataFrame module, and using this apply method:
df['status'] = df['sentiment_rating'].apply(lambda x: 'Positive' if x <= .8 else 'Neutral' if x > -.5 or < .5 else 'Negative' if x < -.5)
Results
I'm getting an Error message of invalid syntax, which doesn't tell me much.
I don't have a clear understanding of the lambda function, and am not even sure if apply is the right way to accomplish my goal.
I've also tried testing with this on 2 dimensions: df['status'] = ['Positive' if x > '.5' else 'other' for x in df['sentiment_rating']], and that's returning Error message TypeError: '>' not supported between instances of 'float' and 'str'
Any input on my approach and what I'm doing wrong greatly appreciated. Thx