Replace value with regex to np.nan

I have a dataframe as shown below:

data1 = {"first":["alice", "bob", "carol"],
         "last_huge":["foo", "bar", "baz"]}
df = pd.DataFrame(data1)

For example, I want to replace all the characters 'o' with 'a':

Then i do

df.replace({"o":"a"},regex=True)
Out[668]: 
   first last
0  alice  faa
1    bab  bar
2  caral  baz

It returns what I need.

However , when I want to replace 'o' with np.nan, it will change the whole line to np.nan. Are there any explanations from the pandas document? . I can find some information through the source code .

Additional information: (The whole line will be changed to np.nan)

df.replace({"o":np.nan},regex=True)
Out[669]: 
   first last
0  alice  NaN
1    NaN  bar
2    NaN  baz
+4
source share
2 answers

NaN , "" , . , NaN- ( , , ), , NaN , .

, :

In [11]: s = pd.Series([1, 2, np.nan, 4])

In [12]: s.sum()
Out[12]: 7.0

In [13]: s.sum(skipna=False)
Out[13]: nan

skipna = False , , NaN . Pandas ...

- NaN?

+3

python cmath.nan math.nan.

CPython: C. F C99, . ValueError , sqrt (-1.0) log (0.0) ( C99 F ) OverflowError , (, exp (1000.0 )). NaN , NaN; NaN, ( C99 F) , pow (float ('nan'), 0.0) hypot (float ('nan'), float ('inf')).

, NaN, NaN

:

, Python NaN NaNs, NaNs . NaN, .

+1

Source: https://habr.com/ru/post/1688191/


All Articles