The Python Oracle

python pandas "groupby" and "if any" condition

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping

--

Chapters
00:00 Python Pandas &Quot;Groupby&Quot; And &Quot;If Any&Quot; Condition
00:33 Accepted Answer Score 4
00:48 Answer 2 Score 2
01:03 Thank you

--

Full question
https://stackoverflow.com/questions/7036...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 4


You could try the following:

df['new_col'] = df.groupby('project')['sector'].transform(lambda x: (x == 'a').any() )

This will group by project and check if any 'a' is in the groups sectors




ANSWER 2

Score 2


It's not the fastest option, but definitely should work.

new_col = your_db.groupby(['project'])['sector'].unique().apply(lambda x: 'a' in x).rename('new_col')
your_db = your_db.merge(new_col, how = 'inner', left_on = 'project', right_on = 'project')