0

Here is a sample dataset:

ID Description
1 he wants some epples
2 she bought 2kgs of bakana
3 he got nothing
4 she took potato and tomat

I wanted to replace it this way:

df['Description']= df['Description'].str.replace({'epples':'apples','bakana':'banana','tomat':'tomato'})

It returned error:

TypeError: replace() missing 1 required positional argument: 'repl'

What can I do to reach this result:

ID Description
1 he wants some apples
2 she bought 2kgs of banana
3 he got nothing
4 she took potato and tomato

2 Answers 2

4

Try like this :

import pandas as pd

df = pd.DataFrame({'ID':[1,2,3,4], 'Description':['he wants some epples', 'she bought 2kgs of bakana', 'he got nothing', 'she took potato and tomat']})
replacement = {
    "epples": "apples",
    "bakana": "banana",
    "tomat": "tomato"
}
print(df['Description'].replace(replacement, regex=True))

Output :

0          he wants some apples
1     she bought 2kgs of banana
2                he got nothing
3    she took potato and tomato
Sign up to request clarification or add additional context in comments.

Comments

0

yes, in str.replace(something, toReplaceWith), you missed toReplaceWith so that it errors

1 Comment

It would be good to provide a correct version of the OP's code that will run without the error they encountered.

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.