简体   繁体   中英

Pandas: writing to original data frame. SettingWithCopyWarning

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).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM