Pandas Set on copy warning when using .loc -


i'm trying change values in column of dataframe based on condition.

in [1]:df.head() out[2]:                gen        cont   timestamp                                                                    2012-07-01 00:00:00        0.293        0     2012-07-01 00:30:00        0.315        0     2012-07-01 01:00:00        0.0          0     2012-07-01 01:30:00        0.005        0     2012-07-01 02:00:00        0.231        0     

i want set 'gen' column nan whenever sum of 2 columns below threshold of 0.01, want this:

in [1]:df.head() out[2]:                gen        cont   timestamp                                                                    2012-07-01 00:00:00        0.293        0     2012-07-01 00:30:00        0.315        0     2012-07-01 01:00:00        nan          0     2012-07-01 01:30:00        nan          0     2012-07-01 02:00:00        0.231        0   

i have used this:

df.loc[df.gen + df.con <0.01 ,'gen'] = np.nan 

it gives me result want warning:

a value trying set on copy of slice dataframe. try using .loc[row_indexer,col_indexer] = value instead 

i confused because using .loc , think i'm using in way suggested.

for me solution works nice.

alternative solution mask, default add nan if condition true:

df['gen'] = df['gen'].mask(df['gen'] + df['cont'] < 0.01) print (df)              timestamp    gen  cont 0  2012-07-01 00:00:00  0.293     0 1  2012-07-01 00:30:00  0.315     0 2  2012-07-01 01:00:00    nan     0 3  2012-07-01 01:30:00    nan     0 4  2012-07-01 02:00:00  0.231     0 

edit:

you need copy.

if modify values in df later find modifications not propagate original data (df_in), , pandas warning.

df = df_in.loc[sdate:edate].copy() 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -