-1

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?

V_sqrt
  • 537
  • 8
  • 28
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – AMC Jan 05 '21 at 01:58

1 Answers1

1

pd.cut is what you want and it is very flexible. Check the documentation to learn more about what it can do.

df['Encoded'] = pd.cut(df['Value'], bins=[0,150,300,100000], labels=['<=150','<150_and_<=300','>300'])
Chris
  • 15,819
  • 3
  • 24
  • 37