Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于熊猫中的bool和float条件创建bool列_Python_Pandas - Fatal编程技术网

Python 基于熊猫中的bool和float条件创建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

我正在努力创造<具有以下逻辑的数据帧中的code>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 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检查更新~