Home > Back-end >  Replace str values in series into np.nan
Replace str values in series into np.nan

Time:07-27

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

CodePudding user response:

You can use

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

1     NaN
2     NaN
3       2
4    2.14
dtype: object

CodePudding user response:

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...

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related