1
         total pts resistance 1 
0          0.5         0.872
1          2.0         1.770
2          0.0         0.246
3          2.0         3.500
4          0.5         1.069
5          1.0         0.793
6          0.5           nan
7          1.5         1.394

I use command like below

>>> df_new['total pts'].add(df_new['resistance 1 '],fill_value=0)

and it return error

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py", line 686, in flex_wrapper
    return self._binop(other, op, level=level, fill_value=fill_value)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/series.py", line 1459, in _binop
    result = func(this_vals, other_vals)
TypeError: unsupported operand type(s) for +: 'float' and 'str'

But it work perfectly with the below command

>>> df_new[['total pts','resistance 1 ']].sum(axis=1)

The issue is i can use sum to replace add but i need to do subtraction, division and multiplication. I want to know why these operator not working.

2
  • what does df.info() show? is that nan a real NaN or the string 'nan'? Commented Mar 17, 2016 at 15:55
  • I think you need cast string to float: print df_new['total pts'].add(df_new['resistance 1'].astype(float), fill_value=0) Commented Mar 17, 2016 at 15:57

1 Answer 1

1

You can try cast string column resistance 1 to float by astype:

print df_new
   total pts resistance 1
0        0.5        0.872
1        2.0         1.77
2        0.0        0.246
3        2.0          3.5
4        0.5        1.069
5        1.0        0.793
6        0.5          nan
7        1.5        1.394

print df_new.dtypes
total pts       float64
resistance 1     object
dtype: object

print type(df_new.loc[0, 'resistance 1 '])
<type 'str'>

After casting you get:

df_new['resistance 1 '] = df_new['resistance 1 '].astype(float)

print df_new.dtypes
total pts        float64
resistance 1     float64
dtype: object

print type(df_new.loc[0, 'resistance 1 '])
<type 'numpy.float64'>
print df_new['total pts'].add(df_new['resistance 1 '].astype(float), fill_value=0)
0    1.372
1    3.770
2    0.246
3    5.500
4    1.569
5    1.793
6    0.500
7    2.894
dtype: float64

If you dont cast, sumonly column total pts, values of column resistance 1 are omited:

print df_new[['total pts','resistance 1 ']].sum(axis=1)
0    0.5
1    2.0
2    0.0
3    2.0
4    0.5
5    1.0
6    0.5
7    1.5
dtype: float64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.