2

When creating a Pandas DataFrame with None values, they are converted to NaN:

> df = pd.DataFrame({'a': [0, None, 2]})
> df

      a
0   0.0
1   NaN
2   2.0

Same thing if I set a value to None by index:

> df = pd.DataFrame({'a': [0, 1, 2]})
> df["a"].iloc[1] = None
> df

      a
0   0.0
1   NaN
2   2.0

However, if I do a replace, weird things start happening:

> df = pd.DataFrame({'a': [0, 1, 2, 3]})
> df["a"].replace(1, "foo")

    a
0   0
1   'foo'
2   2
3   3

> df["a"].replace(2, None)

    a
0   0
1   1
2   1
3   3

What is going on here?

1
  • 1
    df["a"].replace({1:None}) Commented May 14, 2020 at 19:39

2 Answers 2

1
s = pd.Series([10, 'a', 'a', 'b', 'a'])
s.replace({'a': None})
0      10
1    None
2    None
3       b
4    None
dtype: object

s.replace({'a': None}) is equivalent to s.replace(to_replace={'a': None}, value=None, method=None):

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 command s.replace('a', None) is actually equivalent to s.replace(to_replace='a', value=None, method='pad'):

  s.replace('a', None)
    0    10
    1    10
    2    10
    3     b
    4     b
    dtype: object
Sign up to request clarification or add additional context in comments.

Comments

1

According to the doc string

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 command ``s.replace('a', None)`` is actually equivalent to
``s.replace(to_replace='a', value=None, method='pad')``

If you want to actually replace with None, pass a dict:

>>> s = pd.Series([10, 'a', 'a', 'b', 'a'])

When one uses a dict as the `to_replace` value, it is like the
value(s) in the dict are equal to the `value` parameter.
``s.replace({'a': None})`` is equivalent to
``s.replace(to_replace={'a': None}, value=None, method=None)``:

>>> s.replace({'a': None})
0      10
1    None
2    None
3       b
4    None
dtype: object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.