Laurie
Laurie

Reputation: 1219

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

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 >= 1 to positive. The code below, however, produces the following error for 'negative'.

# Function to change values to labels

test.loc[test['sentiment_score'] > 0, 'sentiment_score'] = 'positive'
test.loc[test['sentiment_score'] == 0, 'sentiment_score'] = 'neutral'
test.loc[test['sentiment_score'] < 0, 'sentiment_score'] = 'negative'

Data:                                  Data After Code:
Index     Sentiment                    Index     Sentiment
 0         2                            0         positive
 1         0                            1         neutral
 2        -3                            2         -3
 3         4                            3         positive
 4        -1                            4         -1
 ...                                    ...
 k         5                            k         positive

File "pandas_libs\ops.pyx", line 98, in pandas._libs.ops.scalar_compare TypeError: '<=' not supported between instances of 'str' and 'int

I assume that this has something to do with the function seeing negative numbers as string rather than float/int, however I've tried the following code to correct this error and it changes nothing. Any help would be appreciated.

test['sentiment_score'] = test['sentiment_score'].astype(float)
test['sentiment_score'] = test['sentiment_score'].apply(pd.as_numeric)

Upvotes: 3

Views: 14219

Answers (2)

mellifluous
mellifluous

Reputation: 183

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))

Upvotes: 0

cs95
cs95

Reputation: 403278

As roganjosh pointed out, you're doing your replacement in 3 steps - this is causing a problem because after step 1, you end up with a column of mixed dtypes, so subsequent equality checks start to fail.

You can either assign to a new column, or use numpy.select.

condlist = [
   test['sentiment_score'] > 0,
   test['sentiment_score'] < 0
]
choicelist = ['pos', 'neg']

test['sentiment_score'] = np.select(
   condlist, choicelist, default='neutral')

Upvotes: 6

Related Questions