pandas Python - 'TypeError: '<=' 在 'str' 和 'int' 的实例之间不受支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51249024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:47:15  来源:igfitidea点击:

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

pythonpandas

提问by Laurie

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'.

我有一个 df 列,其值范围从 -5 到 10。我想将值 <= -1 更改为negative,将所有 0 值更改为neutral,并将所有值 >= 1 更改为positive。但是,下面的代码会为“否定”产生以下错误。

# 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

文件“pandas_libs\ops.pyx”,第 98 行,在 pandas._libs.ops.scalar_compare 类型错误:“str”和“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)

回答by cs95

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.

正如 roganjosh 指出的那样,您要分 3 个步骤进行替换 - 这会导致问题,因为在第 1 步之后,您最终会得到一列混合 dtype,因此后续的相等性检查开始失败。

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

您可以分配给新列,也可以使用numpy.select.

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

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

回答by melihozbek

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