I am having difficulties in renaming some values within a column as follows:
- less than or equal to 150;
- between 150 and 300 (included);
- greater than 300.
The column is from a dataframe:
Value
0 146.0
2 148.0
3 158.0
4 207.0
6 196.0
... ...
352 148.0
353 168.0
354 136.0
355 129.0
356 208.0
I have tried as follows:
def group(df):
if (df['Value'].astype(int).notna() <= 150):
return '<=150'
elif ((df['Value'].astype(int).notna() > 150) & (df['Value'].astype(int).notna() <= 300)):
return '>150_and_<=300'
elif (df['Value'].astype(int).notna() > 300):
return '>300'
df['Encoded'] = df.notna().apply(group, axis = 1)
The code above returns this error:
---> 10 df['Encoded'] = df.notna().apply(group, axis = 1) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Can you tell me how to change the code in order to not get the error?