gpt4 book ai didi

python - 如果另一列包含字符串,则替换 pandas 中的一列

转载 作者:行者123 更新时间:2023-11-30 08:58:15 25 4
gpt4 key购买 nike

我有两列 newlabels 和 newlabels_tobeReplaced 。如果 newlabels 的句子中包含单词“trifurcation”,则 newlabels_tobeReplaced 应替换为“trifurcation”我有以下代码

df_new.loc[df_new.newlabels.str.contains('trifurcation'),'newlabels_tobeReplaced'] = 'trifurcation'

但是,我收到此错误:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s

任何想法,如何获得正确的结果。问题是,新标签的值如下:“波形表明左下肢踝关节水平休息时有轻度远端缺血的三叉疾病。”

最佳答案

您可以通过使用 assign 生成的副本重新分配给 df_new 来绕过该警告

df_new = df_new.assign(
newlabels_tobeReplaced=
lambda d: d['newlabels_tobeReplaced'].mask(
d.newlabels.str.contains('trifurcation'), 'trifurcation'
)
)

关于python - 如果另一列包含字符串,则替换 pandas 中的一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51051373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com