Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing specific value on pandas with edited value of row

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

+----------------+
|       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')

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading