1

I understand the idea behind chained assignment, and that when I get the warning

Pandas: SettingWithCopyWarning

it's to let me know that I'm writing to a copy, rather than the original data frame.

But I can't seem to successfully implement .loc or .ix() or .iloc() so that I am actually writing to the original data frame.

In cases where the 'size' column in not null, I want to set the value to null. I've tried:

import numpy as np
df.loc[df['size'].notnull()].value=np.nan

I've also tried playing around with the replace function, but to no avail (the values to be replaced are all zero, so an implementation of .replace(0, np.nan) could also work).

user3591836
  • 953
  • 2
  • 16
  • 29
  • the norm is `df.loc[df['size'].notnull(), 'col'] = np.nan` what is your column name? Note you cannot assign to the `values` attribute – EdChum Oct 28 '14 at 19:43

1 Answers1

2

The below answer is for the original question (before it was edited by the OP). The line of code in question was:

df.loc[df['size'].notnull() & df['value'] == 0].value = np.nan

and I suggested to try this instead (moving value inside the []):

df.loc[df['size'].notnull() & df['value'] == 0, 'value'] = np.nan

EDIT:

this assumes the column name is 'value' and you are not trying to set the values attribute (which as is stated in the comment above, you cannot do)

so, this would work with the following dataframe, for example:

d = {'size' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']),
 'value' : pd.Series([1., 2., 0.], index=['a', 'b', 'd'])}

df = pd.DataFrame(d)
cgc
  • 98
  • 5