1

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?

3
  • You want to replace dummy 1 with dummy 2? Commented Oct 4, 2018 at 5:33
  • Please add more information to this question. Do you want all all values in column 1 to be replaced with the values of column 2? While this can easily be done using 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? Commented Oct 4, 2018 at 5:33
  • I have added more info on what to replace with what. If needed any more please comment here. Commented Oct 4, 2018 at 5:50

1 Answer 1

1

Use nested list comprehension:

df = pd.DataFrame([['3000 20%', 'dummy1 a 3000 dummy2 20%'],
                   ['abc 2%', 'klmn 3000 dummy2 2%']])
print (df)
          0                         1
0  3000 20%  dummy1 a 3000 dummy2 20%
1    abc 2%       klmn 3000 dummy2 2%

df[3] = [' '.join(y for y in j.split() if y not in i.split()) for i, j in zip(df[0], df[1])]
print (df)
          0                         1                 3
0  3000 20%  dummy1 a 3000 dummy2 20%   dummy1 a dummy2
1    abc 2%       klmn 3000 dummy2 2%  klmn 3000 dummy2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.