Reputation: 3159
Let's say we have a DataFrame:
import pandas as pd
df = pd.DataFrame({"a": [1,"NAN",3], "b": ["NAN",5,"NAN"], "c": ["NAN",8,9]})
a b c
0 1 NAN NAN
1 NAN 5 8
2 3 NAN 9
When I try to preform a regular replace
I get incorrect results:
df = df.replace('NAN', None)
a b c
0 1 NAN NAN
1 1 5 8
2 3 5 9
However, when I have the values as lists it works as expected:
df = df.replace(['NAN'], [None])
a b c
0 1.0 NaN NaN
1 NaN 5.0 8.0
2 3.0 NaN 9.0
Can you please explain why the behavior is different in these two cases?
PS: pandas version is 1.0.3.
Upvotes: 0
Views: 332
Reputation: 1151
As was answered here:
df.replace("NAN", None)
is equivalent to df.replace("NAN", None, method='pad')
if you look into the source code.
Both df.replace({"NAN": None})
and df.replace("NAN", np.nan)
give you what you want
Upvotes: 2