Python 基于熊猫中的bool和float条件创建bool列
我正在努力创造<具有以下逻辑的数据帧中的code>bool列:Python 基于熊猫中的bool和float条件创建bool列,python,pandas,Python,Pandas,我正在努力创造bool列: df.a=bool(0或1) df.start=float df.stop=float 如果df.a==1,如果df.start.shift(1)-df.stop>=5,则返回1,否则返回0 我目前正在使用此代码: df['newColumn'] = (df['a'] & (df['start'].shift(1) - df['stop']) >= 5) 但我得到了这个错误: TypeError: unsupported operand ty
df.a
=bool
(0或1)
df.start
=float
df.stop
=float
如果df.a
==1,如果df.start.shift(1)-df.stop>=5,则返回1,否则返回0
我目前正在使用此代码:
df['newColumn'] = (df['a'] & (df['start'].shift(1) - df['stop']) >= 5)
但我得到了这个错误:
TypeError: unsupported operand type(s) for &: 'float' and 'float'
以下是一个示例df:
a start stop
0 .5 1.2
1 1.5 2.2
1 2.3 2.9
0 8.1 8.8
0 17.9 18.1
应该是:
a start stop. newColumn
0 .5 1.2 0
1 1.5 2.2 0
1 2.3 2.9 1
0 8.1 8.8 0
0 17.9 18.1 0
当使用&或链接时,应为每个条件添加()
|
df['newColumn'] = df['a'].astype(bool) & ((df['start'].shift(1) - df['stop']) >= 5)
((df['a'])和((df['start'].shift(1)-df['stop'])>=5))
?@cytorak我得到与以前相同的错误我得到与以前相同的错误before@connor449检查更新~