1
import pandas as pd
import numpy as np
df = pd.DataFrame([[1,2,3],[4,5,6]], columns=['a','b','c'])
df[df['c'] == 3]['a'] = np.nan
df
>>>
    a   b   c
0   1   2   3
1   4   5   6

Why is 0,a not NaN? I would expect that the value in 0,a would have been replaced by np.nan.

DSteman
  • 1,388
  • 2
  • 12
  • 25

1 Answers1

1

Problem is you create chained indexing:

df[df['c'] == 3]

with:

df[df['c'] == 3]['a']

So after set values is not propagate new value to original DataFrame.

For avoid it use DataFrame.loc:

df.loc[df['c'] == 3, 'a'] = np.nan
print (df)
     a  b  c
0  NaN  2  3
1  4.0  5  6

More information.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252