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

Python replace period if it's the only value in column

How do I replace ‘.’ in a dataframe if it’s the only value in a cell without also replacing it if it’s part of a decimal number?

Here’s the dataframe

id
2.2222
.
.
3.2
1.0

I tried this but it removes all decimals

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 = pd.DataFrame({'id':['2.2222','.','.','3.2','1.0']})
df['id'] = df['id'].str.replace('.','',regex=False)

Unwanted result:

id
22222
32
10

This is the desired result:

id
2.2222
3.2
1.0

>Solution :

You could use .loc to do the replace:

df.loc[df['id'] == '.', 'id'] = ''
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