python pandas "groupby" and "if any" condition
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 Question
00:53 Accepted answer (Score 2)
01:13 Answer 2 (Score 2)
01:31 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
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 Question
00:53 Accepted answer (Score 2)
01:13 Answer 2 (Score 2)
01:31 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')