[英]Apply if statement groupby pandas dataframe. The truth value of a Series is ambiguous error
I can not apply an if statement on the column difference_bearing. 我不能在difference_bearing列上应用if语句。 If the values of the column difference_bearing(float) > 20. The expected result would be -1.
如果column_bearing(float)列的值> 20,则预期结果将为-1。 In all other cases the expected results would be 0.
在所有其他情况下,预期结果将为0。
df_FCD_big.groupby(['tripID'])['difference_bearing'].apply(lambda x: -1 if x >20 else 0)
The following warning pops up. 弹出以下警告。
The truth value of a Series is ambiguous. 系列的真实值是不明确的。 Use a.empty, a.bool(), a.item(), a.any() or a.all().
使用a.empty,a.bool(),a.item(),a.any()或a.all()。
Thanks in advance 提前致谢
As mentioned by @coldspeed. 如@coldspeed所提及。
If you need to know metrics within groups, you can do so by applying groupby after that, which would be lot more efficient. 如果您需要了解组内的指标,则可以在此之后应用groupby来做到这一点,这样会更有效率。
df['result'] = np.where(df['difference_bearing'] > 20, -1, 0)
df_FCD_big.groupby(['tripID'])['result'].apply(sum)
这是克服错误的方法:
df_FCD_big.groupby(['tripID'])['difference_bearing'].apply(lambda x: x.apply(lambda y: -1 if y > 20 else 0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.