Reputation:
I have the dataframe:
data = {'in_id': ['28076','28076','28076','28076'],
'name': ['one', 'two', 'three', 'four'],
}
df = pd.DataFrame(data , columns = ['in_id','name'])
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
Upvotes: 0
Views: 107
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