Hello,
I am creating a project in python using Pandas and I want to create a column whose values are the Closed - Open column, but an error occurs that I can not resolve.
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 table rows:
Can you help me?
Thank you.