I need to replacing a specific selected value of row with value that has been edited, what method that I need to do this? For example, I need to replace only the value chosen that contains 'X KODYA'
with a new value 'KOTA X'
For example:
----------------
| A |
----------------
| SURABAYA KODYA |
| JAKARTA |
| KEDIRI KODYA |
----------------
Into:
----------------
| A |
----------------
| KOTA SURABAYA |
| JAKARTA |
| KOTA KEDIRI |
----------------
For now what I do is replacing it manually:
df['A'] = df['A'].str.replace('KEDIRI KODYA', 'KOTA KEDIRI').str.replace('SURABAYA KODYA', 'KOTA SURABAYA')
CodePudding user response:
Use a regex with a capturing group and str.replace
:
df['A'] = df['A'].str.replace(r'(\w )\s KODYA', r'KOTA \1', regex=True)
output:
A
0 KOTA SURABAYA
1 JAKARTA
2 KOTA KEDIRI