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

Replace str values in series into np.nan

I have the following series

s = pd.Series({'A':['hey','hey',2,2.14},index=1,2,3,4)

I basically want to mask, the series and check if the values are a str if so i want to replace then with np.nan, how could i achieve that?

Wanted result

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

s = pd.Series({'A':[np.nan,np.nan,2,2.14},index=1,2,3,4)

I tried this

s.mask(isinstance(s,str))

But i got the following ValueError: Array conditional must be same shape as self, i am kinda a newb when it comes to these methods would appreciate a explanation on the why

>Solution :

You can use

out = s.mask(s.apply(type).eq(str))
print(out)

1     NaN
2     NaN
3       2
4    2.14
dtype: object
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