0

I try to execute this code to replace all df_dash['aoh'] <=262 to standby

if df_dash['aoh'] <= 262:
   df_dash['category'] = 'standby'

However I get this error, and I cannot seem to find a way to get the right results.

Traceback (most recent call last): File "<input>", line 1, in <module> File "/Users/jacob/anaconda3/envs/MERS/lib/python3.7/site-packages/pandas/core/generic.py", line 1555, in __nonzero__ self.__class__.__name__ ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I have read about this error in other threads, but I cannot seem to find a solution to this specific case.

I tried using this as well

df_dash['category'] = np.where(df_dash['aoh'] <= 262, 'standby', df['category'])

But, I get

ValueError: operands could not be broadcast together with shapes (458,) () (4173,) 

Hoping someone could help

meteo_96
  • 289
  • 2
  • 11

3 Answers3

0
 df_dash['category'][df_dash['aoh'] <= 262] = 'standby'

Aiden Zhao
  • 633
  • 4
  • 15
  • it works perfectly, but don't you think it can be just as good to use loc? @YongkangZhao – ansev Sep 18 '19 at 00:58
  • loc is better when the df slice is being pass around functions to make sure the data is being passed as reference in case it needs to be modified inplace – Aiden Zhao Sep 18 '19 at 01:00
0

You can use loc with boolean indexing:

df_dash.loc[df_dash['aoh'] <= 262,'category'] = 'standby'
ansev
  • 30,322
  • 5
  • 17
  • 31
0

Worth nothing - your np.where solution should have worked, but I think you used df['category'] in the second part of the function call, not df_dash['category'].

Can't see the rest of the code but assume the dataframes are different sizes.

elembie
  • 600
  • 2
  • 8