-1

I have a pandas dataframe that goes like below,

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],
                      'seconds' : [21,200,300,400,500,600,700,800]})

I am trying to create buckets based on minutes as below,

def create_bucket(seconds):
    #row['seconds'], buckets- 5,10,15,20,25,30,>30
    if [(seconds >= 0) & (seconds <= 300)]:
        result = "5"
    elif [(seconds >= 301) & (seconds <= 600)]:
        result = "10"
    elif [(seconds >= 601) & (seconds <= 900)]:
        result = "15"
    elif [(seconds >= 901) & (seconds <= 1200)]:
        result = "20"
    elif [(seconds >= 1201) & (seconds <= 1500)]:
        result = "25"
    elif [(seconds >= 1501) & (seconds <= 1800)]:
        result = "30"
    else:
        result = ">30"
    return result

df['Bucket'] = df.apply(lambda row: create_bucket(df['seconds']), axis=1)

The result i get is 5 for all values. I'm trying to figure out what is wrong with the function create_bucket. Any help will be appreciated.

A   seconds Bucket
0   foo 21  5
1   bar 200 5
2   foo 300 5
3   bar 400 5
4   foo 500 5
5   bar 600 5
6   foo 700 5
7   foo 800 5
akhan
  • 9
  • 3

1 Answers1

0

There's not supposed to be square brackets around if conditionals. When you put them there, instead of True or False, you end up with lists: [True] or [False], both of which evaluate to True, since nonempty lists are always truthy. Also, & is bitwise AND, but you want logical AND, so use and instead.

  • Thanks again Joseph. I did that initially but i got the below error, – akhan Apr 21 '20 at 00:01
  • Sorry. The error i receive is, ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0') I got this error earlier and to workaround this error as mentioned in post (https://stackoverflow.com/questions/38834028/use-a-empty-a-bool-a-item-a-any-or-a-all) i added the bitwise & and the square brackets which caused the logic issue as you pointed out. Trying to work out the best way to get this logic going. – akhan Apr 21 '20 at 00:13