First, a MWE comprising two files. The intent is to read the CSV into a pandas dataframe and then rescale all values in each column to the range (-1,1).
data.csv:
Var1,Var2,Var3
2.1,6.4,5.2
7.9,2.1,1.3
5.0,6.1,6.7
mwe.py:
import pandas as pd
import sklearn.preprocessing
data = pd.read_csv("data.csv")
scaler = sklearn.preprocessing.MinMaxScaler(feature_range = (-1, 1))
number_of_columns = data.shape[1]
indices_of_feature_columns = range(0, number_of_columns)
data[indices_of_feature_columns] = scaler.fit_transform(data[indices_of_feature_columns])
When I execute this (Python 2.7.13, sklearn 0.18.1, and pandas 0.20.3), I receive an odd error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mwe.py", line 8, in <module>
data[indices_of_feature_columns] = scaler.fit_transform(data[indices_of_feature_columns])
File "/home/gavin/miniconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 1958, in __getitem__
return self._getitem_array(key)
File "/home/gavin/miniconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 2002, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)
File "/home/gavin/miniconda2/lib/python2.7/site-packages/pandas/core/indexing.py", line 1231, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: '[0 1 2] not in index'
However, when a friend executes this code using a seemingly identical setup, the code runs correctly.
scaler.fit_transform(data[indices_of_feature_columns])throws mentioned exception...