Replace str values in series into np.nan

Question:

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

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

Asked By: INGl0R1AM0R1

||

Answers:

IIUC, You need to create pd.Series like below then use isinstance like below.

import numpy as np
import pandas as pd
s = pd.Series(['hey','hey',2,2.14],index=[1,2,3,4])
s = s.apply(lambda x: np.nan if isinstance(x, str) else x)
print(s)

1     NaN
2     NaN
3    2.00
4    2.14
dtype: float64
Answered By: I'mahdi

Use to_numeric with the errors="coerce" parameter.

s = pd.to_numeric(s, errors = 'coerce')
Out[73]: 
1     NaN
2     NaN
3    2.00
4    2.14
dtype: float64
Answered By: BENY

You can use

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

1     NaN
2     NaN
3       2
4    2.14
dtype: object
Answered By: Ynjxsjmh

If you are set on using mask, you could try:

s = pd.Series(['hey','hey',2,2.14],index=[1,2,3,4])
s.mask(s.apply(isinstance,args = [str]))
print(s)

1     NaN
2     NaN
3       2
4    2.14
dtype: object

But as you can see, many roads leading to Rome…

Answered By: ouroboros1

You could use:

s[s.str.match('D+').fillna(False)] = np.nan

But if you are looking to convert all string ‘types’ not just representations like "1.23" then refer to @Ynjxsjmh’s answer.

Answered By: SomeDude
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.