1

I am trying to use two conditions where one is a range of numbers like the following:

df['COL1'] = np.where(df['COL1'] == 4 & (df['COL2'] in range(28,40)), 20, df['COL1'])

When I run this I get the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Just wondering if there is a way to get this working or something that could be easier than using np.where

validid
  • 13
  • 3

1 Answers1

1

You can use between and also note the parantheses:

np.where((df["COL1"] == 4) & (df["COL2"].between(28, 39)), 20, df["COL1"])

range excludes end-point and between includes by default, hence 39.

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38