0
Hello,
I am creating a project in python using Pandas and I want to create a column whose values are the column Closed - Open, but there is an error that I cannot solve.
My code:
import pandas as pd
dataset = pd.read_csv(r'Documents\Projeto\PETR4.csv', sep=',')
dataset['Date'] = pd.to_datetime(dataset['Date'])
dataset['Variation'] = dataset['Close'].sub(dataset['Open'])
The Error:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-309e31139274> in <module>()
----> 1 dataset['Variation'] = dataset['Close'].sub(dataset['Open'])
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py in flex_wrapper(self, other, level, fill_value, axis)
   1049             self._get_axis_number(axis)
   1050         if isinstance(other, ABCSeries):
-> 1051             return self._binop(other, op, level=level, fill_value=fill_value)
   1052         elif isinstance(other, (np.ndarray, list, tuple)):
   1053             if len(other) != len(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in _binop(self, other, func, level, fill_value)
   1598 
   1599         with np.errstate(all='ignore'):
-> 1600             result = func(this_vals, other_vals)
   1601         name = _maybe_match_name(self, other)
   1602         result = self._constructor(result, index=new_index, name=name)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Example of table rows:
Can you help me?
Thank you.

You can put some sample lines of PETR4.csv sff content?
– Miguel
I edited and put some lines.
– Cristhian Miguel
As images don’t help at all, it makes us have to copy by hand to be able to see the solution... I’ll try to help anyway. Maybe by doing,
pd.read_csv(r'Documents\Projeto\PETR4.csv', sep=',', parse_dates=['Date'])– Miguel
Sorry, I didn’t know you would like the data itself. So the problem is between the Open and Close columns and not Date.
– Cristhian Miguel