0

Code used:

def fn(x):
    for i in x:
        x=x.replace('Wood','Wooden')
        return x


test['Coming:'] = test['Column:'].apply(fn)

Sample output:

Column:       Coming:      Needed:
 Wood         Wooden       Wooden                                        
Wooden       Woodenen      Wooden

I want Wooden and similare categories to be intact like Woodings, woods etc.. Also Column: could be string e.g "Wood is there on the ground" and needed output is "Wooden is there on the ground"

2 Answers 2

1

Here is one way to replace all substrings in a dictionary. Just note that the order may become important if any of the values and keys of the dictionary collide:

import pandas as pd

s = pd.Series(['Wood', 'Wooden', 'Woody Woodpecker', 'wood', 'wood', 'wool suit'])

d = {'Wood': 'Wooden', 'wool': 'soft'}

for k, v in d.items():
    s = s.str.replace(k, v)

# 0                  Wooden
# 1                Woodenen
# 2    Woodeny Woodenpecker
# 3                    wood
# 4                    wood
# 5               soft suit
# dtype: object
Sign up to request clarification or add additional context in comments.

4 Comments

Poignant remark about the dictionary order. But the OP question doesn't even mention a dictionary. I just introduced it in my answer, because I thought one might want to substitute more than one word.
@MrT, I struggled to find a vectorised approach. I hope one exists as this is loopy.
I want Wooden to be intact, but it is getting converted to 'Woodenen'
Can you open a new question? This is going far beyond your original request. That way you can clearly tell us the rules you wish to apply.
1

You can use pandas replace function. Define in a dictionary, what you want to replace and substitute the words in your new column:

import pandas as pd
#test data
df = pd.DataFrame(["Wood", "Wooden", "Woody Woodpecker", "wood", "wool", "wool suit"], columns = ["old"])
#dictionary for substitutions
subst_dict = {"Wood": "Wooden", "wool": "soft"}
df["new"] = df["old"].replace(subst_dict)
#output
                old               new
0              Wood            Wooden
1            Wooden            Wooden
2  Woody Woodpecker  Woody Woodpecker
3              wood              wood
4              wool              soft
5         wool suit         wool suit

Though for more complex substitutions utilising regex, it might be a good idea to write a function and use your apply() approach.

Update after changing the requirements:
If you want to match only whole words in phrases, you can use regex:

import pandas as pd
#test data
df = pd.DataFrame(["Wood", "Wooden", "Woody Woodpecker", "wood", "wool", "wool suit", "Wood is delicious", "A beautiful wool suit"], columns = ["old"])
#dictionary for substitutions
subst_dict = {"Wood": "Wooden", "wool": "soft"}
#create dictionary of regex expressions
temp_dict = {r'(\b){}(\b)'.format(k) : v for k, v in subst_dict.items()}
#and substitute
df["new"] = df["old"].replace(temp_dict, regex = True)
#output
                     old                    new
0                   Wood                 Wooden
1                 Wooden                 Wooden
2       Woody Woodpecker       Woody Woodpecker
3                   wood                   wood
4                   wool                   soft
5              wool suit              soft suit
6      Wood is delicious    Wooden is delicious
7  A beautiful wool suit  A beautiful soft suit

2 Comments

but it doesn't work on String. e.g. if old= "Wood is there in the garden" What I want is "Wooden is there in the garden" Please help its urgent
Please don't change your question by adding new rules. It is rather suggested to ask a new question.

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.