Issue
I wanted to replace np.nan
with None
values, but weird behaviour occurred:
>>> import pandas as pd
>>> import numpy as np
>>> s = pd.Series(['cat', 'dog', np.nan, 'rabbit'])
>>> s = s.replace(np.nan, None)
>>> print(s)
0 cat
1 dog
2 dog
3 rabbit
dtype: object
How can np.nan
be replaced with 'dog'
string? I do not understand. Can you explain it to me, please?
I found out that this code works as expected, so the right solution answers are not necessary.
s = s.replace({np.nan: None})
Solution
According to documentation:
>>> s = pd.Series([10, 'a', 'a', 'b', 'a'])
When
value=None
and to_replace is a scalar, list or tuple, replace uses the method parameter (default ‘pad’) to do the replacement. So this is why the ‘a’ values are being replaced by 10 in rows 1 and 2 and ‘b’ in row 4 in this case. The commands.replace('a', None)
is actually equivalent tos.replace(to_replace='a', value=None, method='pad')
:>>> s.replace('a', None) 0 10 1 10 2 10 3 b 4 b dtype: object
Answered By - Jaroslav Bezděk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.