The Python Oracle

Check if a value exists using multiple conditions within group in pandas

--------------------------------------------------
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 2 Looping

--

Chapters
00:00 Check If A Value Exists Using Multiple Conditions Within Group In Pandas
00:36 Accepted Answer Score 16
01:10 Answer 2 Score 4
01:34 Thank you

--

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

--

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

--

Tags
#python #pandas #numpy

#avk47



ACCEPTED ANSWER

Score 16


Use groupby on Group column and then use transform and lambda function as:

g = df.groupby('Group')
df['Expected'] = (g['Value1'].transform(lambda x: x.eq(7).any()))&(g['Value2'].transform(lambda x: x.eq(9).any()))

Or using groupby, apply and merge using parameter how='left' as:

df.merge(df.groupby('Group').apply(lambda x: x['Value1'].eq(7).any()&x['Value2'].eq(9).any()).reset_index(),how='left').rename(columns={0:'Expected_Output'})

Or using groupby, apply and map as:

df['Expected_Output'] = df['Group'].map(df.groupby('Group').apply(lambda x: x['Value1'].eq(7).any()&x['Value2'].eq(9).any()))

print(df)
   Group  Value1  Value2  Expected_Output
0      1       3       9             True
1      1       7       6             True
2      1       9       7             True
3      2       3       8            False
4      2       8       5            False
5      2       7       6            False



ANSWER 2

Score 4


You can create a dataframe of the expected result by group and then merge it back to the original dataframe.

expected = (
    df.groupby('Group')
    .apply(lambda x: (x['Value1'].eq(7).any() 
                      & x['Value2'].eq(9)).any())
    .to_frame('Expected_Output'))
>>> expected
       Expected_Output
Group                 
1                 True
2                False

>>> df.merge(expected, left_on='Group', right_index=True)
   Group  Value1  Value2  Expected_Output
0      1       3       9             True
1      1       7       6             True
2      1       9       7             True
3      2       3       8            False
4      2       8       5            False
5      2       7       6            False