python - Pandas logical indexing on a single column of a dataframe to assign values -
i r programmer , looking similar way in r:
data[data$x > value, y] <- 1
(basically, take rows x column greater value , assign y column @ rows value of 1)
in pandas seem equivalent go like:
data['y'][data['x'] > value] = 1
but gives settingwithcopywarning.
equivalent statements i've tried are:
condition = data['x']>value data.loc(condition,'x')=1
but i'm confused. maybe i'm thinking in r terms , can't wrap head around what's going on in python. equivalent code in python, or workarounds?
your statement incorrect should be:
data.loc[condition, 'x'] = 1
example:
in [3]: df = pd.dataframe({'a':np.random.randn(10)}) df out[3]: 0 -0.063579 1 -1.039022 2 -0.011687 3 0.036160 4 0.195576 5 -0.921599 6 0.494899 7 -0.125701 8 -1.779029 9 1.216818 in [4]: condition = df['a'] > 0 df.loc[condition, 'a'] = 20 df out[4]: 0 -0.063579 1 -1.039022 2 -0.011687 3 20.000000 4 20.000000 5 -0.921599 6 20.000000 7 -0.125701 8 -1.779029
as subscripting df should use square brackets []
rather parentheses ()
function call. see docs
Comments
Post a Comment