Unsupported operand in pandas data mode

I have a pandas DataFrame. I would like to get one value from a column based on a condition associated with two other columns. I am looking for the value from column3 that has the largest spacing in columns1 and 2.

I am building a simple example that 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 result from this example looks like what 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 am trying to apply exactly the same logic to my original problem with a large dataframe inside a class. Code:

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

      

but this code throws 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 here that there might be a problem with the type of columns, but Depth is type numpy.float64

Hper is type float

Vper is type float

so I understand how this might apply to my problem.

I don't know what to do from now on, as I understand that the same code works in one case but not in the other, and I cannot detect the problem.

+3


source to share


1 answer


You have multiple lines in DenFrame.Hper

and DenFrame.Vper

.

You can see this by checking dtype

or the type of each element:

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

      

Means numpy array can contain different types, we can see that these types are:

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

      

And you can check which records are strings:



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

      

It might make sense to include only those that are floats:

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

      

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

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

      

+9


source







All Articles