Python - 'TypeError: '<=' not supported between instances of 'str' and 'int''

前端 未结 2 819
暖寄归人
暖寄归人 2021-01-06 12:43

I have a df column that has values ranging from -5 to 10. I want to change values <= -1 to negative, all 0 values to neutral, and all values >=

2条回答
  •  旧巷少年郎
    2021-01-06 13:02

    Another alternative is to define a custom function:

    def transform_sentiment(x):
        if x < 0:
            return 'Negative'
        elif x == 0:
            return 'Neutral'
        else:
            return 'Positive'
    
    df['Sentiment_new'] = df['Sentiment'].apply(lambda x: transform_sentiment(x))
    

提交回复
热议问题