Home > Blockchain >  Create new column using custom function pandas df error
Create new column using custom function pandas df error

Time:12-03

I want to create a new column which gives every row a category based on their value in one specific column. Here is the function:

def assign_category(df):
  if df['AvgVAA'] >= -4:
    return 'Elite'
  elif df['AvgVAA'] <= -4 and df['AvgVAA'] > -4.5:
    return 'Above Average'
  elif df['AvgVAA'] <= -4.5 and df['AvgVAA'] > -5:
    return 'Average'
  elif df['AvgVAA'] <= -5 and df['AvgVAA'] > -5.5:
    return 'Below Average'
  elif df['AvgVAA'] <= -5.5:
    return 'Mediocre'

I get this error message in the second line: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

This is the code to create the new column:

df_upd['VAA Category'] = df_upd.apply(assign_category(df_upd), axis = 1)

I did some research on it, but the explanation did not help me because it was mainly about and operators, which are not used in that line.

I do not know why, but the error message did not come up when I first ran the code. But even at that time, the function did not work. Every row in that new column was filled with 'Unknown'.

Can someoen help me out here?

CodePudding user response:

You're so close. Instead of:

df_upd.apply(assign_category(df_upd), axis = 1)

Use:

df_upd.apply(assign_category, axis = 1)

In the updated approach, you are applying the function to df_upd (as intended), whereas in the original approach, you are essentially doing:

x = assign_category(df)
df.apply(x, axis = 1)

The error comes when you try to calculate x since you need to apply along axis 1.

  • Related