![](/img/trans.png)
[英]Python Pandas how to update a column if another column contains a certain string
[英]Replace a column in pandas if another column contains a string
我有兩列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
任何想法,如何獲得正確的結果。 問題在於新標簽的價值如下:“波形提示在足踝水平靜息時左下肢輕度遠端缺血的三叉神經病變疾病。”
您可以通過重新分配來解決這一警告df_new
從制作拷貝assign
df_new = df_new.assign(
newlabels_tobeReplaced=
lambda d: d['newlabels_tobeReplaced'].mask(
d.newlabels.str.contains('trifurcation'), 'trifurcation'
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.