How do I replace string values in a dataframe column[1] using list of string values in a different column[2].
Data
0 1 2 3
0 3000 20% dummy1 3000 dummy2 20% [3000, 20%] dummy1 dummy2
I want to replace string value in column 1 i.e. "dummy1 3000 dummy2 20%" using list in column 2 i.e. "[3000, 20%]". So 3000 and 20% are replaced with ""(empty string) from the string to form 3rd column(Result) i.e. "dummy1 dummy2"
Code
df = pd.DataFrame([['3000 20%', 'dummy1 3000 dummy2 20%']])
df[2] = df[0].str.split(' ')
def replace_string(x):
repl_string = str(x[1])
for key in x[2]:
repl_string = repl_string.replace(key, '')
return ' '.join(repl_string.split())
df[3] = df.apply(replace_string, axis=1)
I have currently written the above code, which is slow for large dataframe. How do I improve the efficiency of this code or is there any other way to do this?
df[1] = df[2].apply(lambda x: x), it does not make any sense to do, so the question is are you trying to replace null values or what?