[英]Python using ternary operator in groupby
Is there a way to use the ternary operator inside a groupby condition?Apparently this syntax is invalid. 有没有办法在groupby条件中使用三元运算符?显然这种语法无效。
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 ''
You can use a ternary, but I'm not going to show you that. 你可以使用三元组,但我不打算告诉你。 Instead, here's a better alternative - mask the result:
相反,这是一个更好的选择 - 掩盖结果:
v = df2.groupby(['name','col2'])['col4'].max()
v.where(v.gt(30), '')
Using lambda
s inside groupby
slows its operation down quite drastically. 在
groupby
内部使用lambda
可以大大减慢其操作速度。
If you want to insist on using a ternary, the below would do: 如果你想坚持使用三元,下面会做:
def condition(row):
return row['col4'].max() if row['col4'].max() > 30 else ''
ds = df2.groupby(['name','col2']).apply(lambda row: condition(row))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.