使用多列进行列表理解 [英] list comprehension using multiple columns

查看:42
本文介绍了使用多列进行列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas数据框,其中包含一列用于表示实际值和预测值的列.我想使用列表推导创建一个新列,当实际值=预测时= 1,否则返回0.我知道如何使用np.where来做到这一点,但我很好奇如何使用列表理解来做到这一点.

I have a pandas data frame with a column for actuals, and predicted. I would like to make a new column using list comprehension that = 1 when actuals = predicted, 0 otherwise. I know how to do this using np.where, but I was curious to know how to do it using list comprehension.

这可以在np.where下使用:

combined['correct'] = np.where(combined.actual==combined.predicted, 1, 0)

谢谢!

推荐答案

您不需要np.where,也不需要列表理解:

You don't need np.where nor list comprehension:

您可以使用此:

combined['correct'] = (combined.actual == combined.predict).mul(1)

combined['correct'] = (combined.actual == combined.predict).astype(int)

这篇关于使用多列进行列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆