MG Fern
MG Fern

Reputation: 119

Python remove text if same of another column

I want to drop in my dataframe the text in a column if it starts with the same text that is in another column. Example of dataframe:

name        var1
John Smith  John Smith Hello world
Mary Jane   Mary Jane Python is cool
James Bond  My name is James Bond
Peter Pan   Nothing happens here

Dataframe that I want:

name        var1
John Smith  Hello world
Mary Jane   Python is cool
James Bond  My name is James Bond
Peter Pan   Nothing happens here

Something simple as:

df[~df.var1.str.contains(df.var1)]

does not work. How I should write my python code?

Upvotes: 0

Views: 332

Answers (2)

Kamil Oster
Kamil Oster

Reputation: 51

How about this?

df['var1'] = [df.loc[i, 'var1'].replace(df.loc[i, 'name'], "") for i in df.index]

Upvotes: 1

Sachin Kohli
Sachin Kohli

Reputation: 1986

Try using apply lambda;

df["var1"] = df.apply(lambda x: x["var1"][len(x["name"]):].strip() if x["name"] == x["var1"][:len(x["name"])] else x["var1"],axis=1)

Upvotes: 1

Related Questions