我有两列 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' ) )