比较python中的数据帧列值

时间:2017-09-03 16:33:29

标签: python pandas dataframe

我需要有关数据帧值比较的帮助。 我有n行的数据帧。我想比较最后三个col值应该大于之前的

if(data["percent"].item()>data["percent"].shift().item()>data["percent"].shift(-1).item()):

我在执行时遇到错误。

if(data["persent"].item()>data["persent"].shift().item()>data["persent"].shift(-1).item()):
  File "C:\Anaconda3\lib\site-packages\pandas\core\base.py", line 827, in item
    return self.values.item()
ValueError: can only convert an array of size 1 to a Python scalar

1 个答案:

答案 0 :(得分:0)

首先,删除libc++abi.dylib: terminating with uncaught exception of type NSException ,因为这仅适用于单个大小的数组。接下来,您需要更改.items()的结构,因为在python中

if

被翻译为

x > y > z 

但是,x > y and y > z 运算符与pandas无效,因为pandas列不是标量。您必须使用为此目的而重载的按位and / & / |运算符。试试这个:

~

现在,比较结果是真/假向量的掩码,但是if ((data["persent"] > data["persent"].shift()) & (data["persent"].shift() > data["persent"].s‌​hift(-1))).any(): ... # something here 无法理解这一点。因此,您需要使用if.all()将其缩减为单个标量布尔值,具体取决于您的业务逻辑。

相关问题