I'm not sure you even need np.where
here. To element-wise and
two series, use &
here instead of and
. See: Logical operators for boolean indexing in Pandas
Also, 3750901.5068 <= train["x"] <= 3770901.5068
seems to be internally translated by python into (3750901.5068 <= train["x"]) and (train["x"] <= 3770901.5068)
, which again, has and
and won't work. So you'll need to either explicitly split each one up into e.g. (3750901.5068 <= train["x"]) & (train["x"] <= 3770901.5068)
or use Series.between
e.g. train["x"].between(3750901.5068, 3770901.5068, inclusive=True)
. See: How to select rows in a DataFrame between two values, in Python Pandas?
You'll also need parentheses for the two arguments to &
.
So the end result should look like
train["location"] = train["x"].between(3750901.5068, 3770901.5068, inclusive=True) & train['y'].between(-19268905.6133, -19208905.6133, inclusive=True)
This will give you a series of bools (Trues and Falses). These are already just 0s and 1s under-the-hood. If you really want 0s and 1s, you can pick a solution from here. For example, train.location = train.location.astype(int)