jeudi 28 juin 2018

error in element existing identification when using 'if in'

Here is my code:

df1 = pd.DataFrame({'a': [1,2,3,1,2,3,3],'b':[1,2,3,1,2,3,3],'type':[1,0,1,0,1,0,1]})
def add_buy_label(group):
    behavior_type = group.type.astype(int)
    if 1 in group['type']:
        group['buy_label'] = 1
    else:
        group['buy_label'] = 0

    return group[['a', 'b', 'type','buy_label']]

The functions above is to make the buy_label to 1 for all the a-b item as long as one (type = 1) in the group exist, however, the result after

df1.groupby(['a','b'],as_index = False).apply(add_buy_label)

is

    a  b  type  buy_label
0  1  1     1          0
1  2  2     0          1
2  3  3     1          0
3  1  1     0          0
4  2  2     1          1
5  3  3     0          0
6  3  3     1          0

It is pretty obvious that the row with 3 is wrong, because there is type = 1 existing in the group of (a=3,b=3), but the according buy_label is 0.

How can I fix it ?

Aucun commentaire:

Enregistrer un commentaire