Skip to content Skip to sidebar Skip to footer

Remove Substring From Column Based On Another Column

Attempting to use the values (as string) from one column to determine what gets removed from another column. Remainder of the column must be unchanged. Example data: import pandas

Solution 1:

So, I tried this and it worked pretty well:

dfTest['bar'] = dfTest.apply(lambda row : row['foo'].replace(str(row['date']), ''), axis=1)

Solution 2:

Eddited: I noticed that with replace on lambda it wasn't working as expected so I split into a function.

defreplace(str1, str2):
    return str1.replace(str2, '')


dfTest['bar'] = dfTest.apply(lambda row: replace(row['foo'], row['date']), axis=1)

Post a Comment for "Remove Substring From Column Based On Another Column"