0

My dataframe df_reps look like this

        RepID  TermCnt 
0           1       12 
1           1        4 
2           1        3 
3           1        4 
4           1        2 
      ...      ...  ...
1116984  4999        3 
1116985  4999        2 
1116986  4999        1 
1116987  4999        2 
1116988  4999        1 

I am trying to create a new column called Cat

using

df_reps["Cat"] = df_reps["TermCnt"] if df_reps["TermCnt"] < 3 else 99

but getting this error

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

I checked this link

Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

and it is seems there is a non logical if statement

but when I try this line

df_reps["TermCnt"] < 3

I get these values

df_reps["TermCnt"] < 3
Out[96]: 
0          False
1          False
2          False
3          False
4           True
 
1116984    False
1116985     True
1116986     True
1116987     True
1116988     True
Name: TermCnt, Length: 1116989, dtype: bool

which means the logical part is correct

any idea how to fix that?

asmgx
  • 7,328
  • 15
  • 82
  • 143

1 Answers1

1

You can use apply() method:

df_reps["Cat"]=df_reps["TermCnt"].apply(lambda x:x if x < 3 else 99)

Now if you print df_reps you will get your desired output

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • WHen I expand the statement i get the same error df_reps["Cat"]=df_reps["TermCnt"].apply(lambda x:x if x < 3 else df_reps["Perc25"] if x<=df_reps["Perc25"] else df_reps["Perc50"] if x<=df_reps["Perc50"] else df_reps["Perc75"] if x<=df_reps["Perc75"] else df_reps["Perc100"] ) – asmgx Apr 14 '21 at 02:12
  • 1
    i found the reason, it is actually because i am using df_reps["Perc25"] instead of x["Perc25"] etc. – asmgx Apr 14 '21 at 03:30