Ara
Ara

Reputation: 145

Replace a column in pandas if another column contains a string

I have two columns newlabels and newlabels_tobeReplaced . If newlabels contains the word 'trifurcation' in its sentence, newlabels_tobeReplaced should be replaced to 'trifurcation' I have the following code

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

But , I get this error:

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

Any idea, how to get the correct result. The problem is that the newlabels has got values like : "Waveforms suggest trifucation disease with mild distal ischemia of left lower extremity at rest at level of ankle."

Upvotes: 1

Views: 656

Answers (1)

piRSquared
piRSquared

Reputation: 294338

You can get around that warning by reassigning to df_new with the copy produced from assign

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

Upvotes: 2

Related Questions