Python-'str'和'int'的实例之间不支持'TypeError:'< ='
我有一个df列,其值的范围是-5到10.我想将值negative,将所有0值更改为neutral
,并将所有值> = 1更改为
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
pandas._libs.ops.scalar_compare中的文件"pandas_libs \ ops.pyx",第98行 TypeError:"str"和"int
File "pandas_libs\ops.pyx", line 98, in pandas._libs.ops.scalar_compare TypeError: '<=' not supported between instances of 'str' and 'int
我认为这与将负数视为字符串而不是float/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)
正如roganjosh所指出的,您要分3步进行替换-这引起了问题,因为在第1步之后,您会遇到混合的列dtypes,因此后续的相等性检查开始失败.
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.
您可以分配给新列,也可以使用 numpy.select
.
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')