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

How to replace will work in pandas dataframe?

Hi I have dataframe in which I have to replace certain thing but not with all values. Please help how can achieve. Please see below example-

Example-

df:

Col
N//A
P//D
https://cg.com
C//A

Final Output-

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

df:

Col
N/A
P/D
https://cg.com
C/A

I used df.replace('//', '/', inplace=True) but it is replacing all.

>Solution :

Use a regex with lookbehind to exclude the replacement if the // is preceded by ::

df.replace(r'(?<!:)//', '/', regex=True, inplace=True)

Output:

              Col
0             N/A
1             P/D
2  https://cg.com
3             C/A
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