使用python比较多列? [英] Comparing multiple columns using python?

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

问题描述

我正在尝试比较多个列,并将布尔值分配给名为 New Column 的新列.请在下面找到示例数据.

I am trying to compare multiple columns and assign the boolean value to a new column called New Column. Please find the sample data below.

df = pd.DataFrame({
    'date1':['2000-03-01', '2000-03-02'], 
    'date2':['2000-04-01', '2000-02-02'],
    'date3':['2000-05-01','2000-03-02']
})
print(df)

这是我的尝试:

df['New Column'] = '0'
df.loc[df['date1'] <= df['date2'] <=  df['date3'] , 'New Column'] = '1'

但是,这现在可以正常工作,并且给我错误 ValueError:系列的真值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all().

However, this is now working and giving me error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

为什么会发生这种情况,请问如何解决?在其他编程语言中,这似乎不是问题,但是在python中.有人可以帮我理解这里的问题吗?

Why is this happening and how can I solve it please? This does not seem to be a problem in other programming languages but in python. Can anyone help me understand the problem here?

推荐答案

使用&条件.

示例代码在这里.

df['New Column'] = '0'
df.loc[(df['date1'] <= df['date2']) & (df['date2'] <=  df['date3']), 'New Column'] = '1'

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

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