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:
JavaScript
x
2
1
df['Description']= df['Description'].str.replace({'epples':'apples','bakana':'banana','tomat':'tomato'})
2
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 |
Advertisement
Answer
Try like this :
JavaScript
1
10
10
1
import pandas as pd
2
3
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']})
4
replacement = {
5
"epples": "apples",
6
"bakana": "banana",
7
"tomat": "tomato"
8
}
9
print(df['Description'].replace(replacement, regex=True))
10
Output :
JavaScript
1
5
1
0 he wants some apples
2
1 she bought 2kgs of banana
3
2 he got nothing
4
3 she took potato and tomato
5