Python uses ternary operators in groupby … here is a solution to the problem.
Python uses ternary operators in groupby
Is there a way to use ternary operators in groupby conditions? Obviously this syntax is invalid.
d = {'name':['bil','bil','bil','jim'],
'col2': ['acct','law', 'acct2','law'],
'col3': [1,2,3,55],
'col4': [1,1,1,2]
}
df2 = pd. DataFrame(data=d)
df2[['col4']] = df2[['col4']].apply(pd.to_numeric)
df2.groupby(['name','col2'])['col4']\
.max() if (.max()>30) else ''
Solution
You can use ternary, but I won’t show you. Instead, there’s a better option here – mask the results:
v = df2.groupby(['name','col2'])['col4'].max()
v.where(v.gt(30), '')
Using a lambda
in Groupby
significantly slows it down.