2

Trying to change 174.0 to NaN. Am I missing something obvious? Finding the index of the value in the overall dataframe is too complicated, so I narrowed it down to Well L15. Is this not allowd?

input: df[df['Well']=='L15'].iloc[4,6]
output: 174.0

input: df[df['Well']=='L15'].iloc[4,6] = np.nan 

input: df[df['Well']=='L15'].iloc[4,6]
output: 174.0

I expect this to give me NaN at the end, not 174.0. Thank you!

  • 1
    Are you getting a [`SettingWithCopy Warning`](https://stackoverflow.com/q/21463589/14627505) by any chance? https://pandas.pydata.org/docs/user_guide/indexing.html?highlight=chained#returning-a-view-versus-a-copy – Vladimir Fokow Aug 26 '22 at 19:19

1 Answers1

0

The docs say:

Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees)

So in your case, it is hard to predict, will df[df['Well']=='L15'].iloc[4,6] be a

  • view (in which case changing its elements would change the original elements since it is only a view),
  • or a copy (in which case its elements would be changed, but the original elements would not.)

Here is a workaround that you could use. Create a copy explicitly (so that pandas doesn't complain for not knowing if it's a view or a copy), change the value in this copy, and then replace the values in the original:

sub_df = df[df['Well']=='L15'].copy()

sub_df.iloc[4, 6] = np.nan 
df[df['Well']=='L15'] = sub_df
Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27