user15920209
user15920209

Reputation:

error bool has no attribute shift, shift doesn't work

I have the dataframe:

data = {'in_id': ['28076','28076','28076','28076'],
'name': ['one', 'two', 'three', 'four'],  
}
df = pd.DataFrame(data , columns = ['in_id','name'])

enter image description here

So, i try to run df.groupby('in_id').apply(lambda x: x[(x.name=='four').shift(-1).fillna(False)]) but i get an error 'bool' object has no attribute 'shift'

How do i can to run it without getting an error? Can anyone see the problem

Output dataframe

enter image description here

Upvotes: 0

Views: 107

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24322

Try:

out=df[df['name'].eq('four').shift(-1,fill_value=False)]

OR(If there is particular reason of using groupby())

out=(df.groupby('in_id')['name']
   .agg(lambda x: x[x.eq('four').shift(-1,fill_value=False)])
   .reset_index())
#you can also use apply() in place of agg() method

Now If you print out you will get your desired output

Upvotes: 1

Related Questions