Trim string in column with substring in other column

Question:

df = pd.DataFrame({
  "string": ["abc", "def", "ghi"],
  "substring": ["bc", "e", "ghi"]
})

Is an operation like this possible?

df['string']=df['string'].replace(df['substring'], r'')

expected result:

string substring 
a      bc        
df     e        
       ghi       
Asked By: Blob

||

Answers:

Use lsit comprehension:

df['string']= [a.replace(b, '') for a, b in zip(df['string'], df['substring'])]
print (df)
  string substring
0      a        bc
1     df         e
2              ghi
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.