pandas 熊猫数据帧操作中不支持的操作数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14450020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:36:33  来源:igfitidea点击:

unsupported operand in pandas dataframe operation

pythonpandas

提问by tomasz74

I have pandas DataFrame. I would like to get single value from a column based on a condition involving two another column. I am looking for the value from the column3 for which is the biggest distance in the column1 and 2.

我有Pandas数据框。我想根据涉及另外两列的条件从一列中获取单个值。我正在寻找 column3 中的值,该值是 column1 和 2 中的最大距离。

I build the simple example which works:

我构建了一个简单的例子:

d = pd.DataFrame({'c1':[.1,3,11.3],'c2':[3,6,.6],'c3':[8,.8,10.9]})
print'data d=\n%s\n' % d                                               
x = float(d.c3[abs(d.c1-d.c2)==max(abs(d.c1-d.c2))].values)
print 'the value of x= \n%s\n' % x

The output from this example is as I expect:

这个例子的输出正如我所期望的:

     c1   c2    c3
0   0.1  3.0   8.0
1   3.0  6.0   0.8
2  11.3  0.6  10.9

the value of x= 
10.9

I try to apply exactly the same logic to my original problem with large dataframe inside a class. The code is:

我尝试将完全相同的逻辑应用于我在类中使用大数据框的原始问题。代码是:

yInit = float(self.DenFrame.Depth[abs(self.DenFrame.Hper-self.DenFrame.Vper)==max(abs(self.DenFrame.Hper-self.DenFrame.Vper))].values)

but this code produce an error:

但此代码产生错误:

...
  File "C:\Python27\lib\site-packages\pandas-0.9.0-py2.7-win32.egg\pandas\core\series.py", line 73, in wrapper
return Series(na_op(self.values, other.values),
  File "C:\Python27\lib\site-packages\pandas-0.9.0-py2.7-win32.egg\pandas\core\series.py", line 59, in na_op
result[mask] = op(x[mask], y[mask])
TypeError: unsupported operand type(s) for -: 'str' and 'str'

I found in herethat there could be a problem with type of the columns but Depth is type numpy.float64Hper is type floatVper is type floatso I understand how it can apply to my problem.

我在这里发现列的类型可能有问题,但深度是numpy.float64Hper 类型,floatVper 是类型,float所以我了解它如何适用于我的问题。

I don't know from this point what to do as I understand the same code works in one case but not in another and I cannot spot the problem.

从这一点上我不知道该怎么做,因为我知道相同的代码在一种情况下有效,但在另一种情况下无效,而且我无法发现问题。

回答by Andy Hayden

You have some strings in your DenFrame.Hperand DenFrame.Vper.

您的DenFrame.Hperand 中有一些字符串DenFrame.Vper

You can see this by checking the dtypeor type of each element:

您可以通过检查dtype每个元素的or 类型来查看这一点:

In [11]: df.Hper.dtype
Out[11]: dtype('object')

Means that the numpy array could contain various types, we can see what these types are:

意味着 numpy 数组可以包含各种类型,我们可以看到这些类型是什么:

In [12]: DenFrame.Hper.map(type).unique()
Out[12]: [<type 'float'> <type 'str'>]

And you could inspect which entries are strings:

您可以检查哪些条目是字符串:

DenFrame[DenFrame.Hper.map(type) == str]

Perhaps it makes sense to only include those which are floats:

也许只包括那些浮动的东西是有意义的:

DenFrame_floats = DenFrame[(DenFrame.Hper.map(type) == float) & 
                           (DenFrame.Vper.map(type) == float)]

or you could (if it's possible) convert them to floats:

或者您可以(如果可能)将它们转换为浮点数:

DenFrame.Hper = DenFrame.Hper.apply(float)