You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas处理同一行两列重复值:相等时将label列替换为same

错误原因
  • 你传入np.where的第一个参数用了df.query(),该方法返回的是符合条件的子DataFrame,长度和原表不一致,直接触发长度不匹配报错。
  • np.where后两个参数应为匹配条件后要赋值的实际内容,你写的是相等判断表达式,返回的是布尔值,不符合业务需求。
修复后的实现代码
import numpy as np
import pandas as pd

df = pd.DataFrame({'text':['she is good', 'she is bad'], 'label':['she is good', 'she is good']})
# 逐行判断替换label值
df['label'] = np.where(df['text'] == df['label'], 'same', df['label'])
# 若需要将text列重命名为示例输出的pos列,补充以下代码
df = df.rename(columns={'text': 'pos'})
print(df)
输出结果
pos        label
0  she is good         same
1   she is bad  she is good

内容的提问来源于stack exchange,提问作者zara kolagar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 18:36:01