I would like to know how to replace all the strings I want with nan values

Asked 2 years ago, Updated 2 years ago, 16 views

s3 = Series ('Seoul', '', 'Daejeon', 'Daegu', '!', 'Busan')


s3.place('Seoul', 'Daejeon', 'Daegu', 'Busan', np.nan)

I know how to change Seoul, Daejeon, Daegu, and Busan to Nan I don't know how to change everything to nan values except Seoul, Daejeon, Daegu, and Busan.

python

2022-09-20 11:36

1 Answers

>>> import pandas as pd
>>> s = pd.Series ("Seoul Daejeon Daegu! Busan".split())
>>> s
0 Seoul
the first battle
2 Daegu
3     !
4 Busan
dtype: object
>>> s[s.isin ("Seoul, Daejeon, Daegu, Busan").split())]
0 Seoul
the first battle
2 Daegu
4 Busan
dtype: object

>>> s[!s.isin ("Seoul, Daejeon, Daegu, Busan").split())]
SyntaxError: invalid syntax

>>> s[nots.isin] ("Seoul, Daejeon, Daegu, Busan").split())]
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    s[nots.isin ("Seoul, Daejeon, Daegu, Busan").split())]
  File "C:\PROGRAMS\Python3864\lib\site-packages\pandas\core\generic.py", line 1537, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


>>> s[~s.isin ("Seoul, Daejeon, Daegu, Busan").split())]
3    !
dtype: object
>>> import numpy as np
>>> s[~s.isin ("Seoul, Daejeon, Daegu, Busan").split())] = np.nan
>>> s
0 Seoul
the first battle
2 Daegu
3    NaN
4 Busan
dtype: object
>>> 


2022-09-20 11:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.