python - 类型错误 : cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index' >

标签 python

我正在尝试运行有关成人人口普查数据的示例代码。 当我运行这段代码时:

X_train, X_test, y_train, y_test = cross_validation.train_test_split(encoded_data[encoded_data.columns - ["Target"]], encoded_data["Target"], train_size=0.70, random_state = 42)
scaler = preprocessing.StandardScaler()
X_train = pd.DataFrame(scaler.fit_transform(X_train.astype("f64")), columns=X_train.columns)
X_test = scaler.transform(X_test.astype("f64"))

我一直收到这个错误:

  2101     def __sub__(self, other):
   2102         raise TypeError("cannot perform __sub__ with this index type: "
-> 2103                         "{typ}".format(typ=type(self)))
   2104 
   2105     def __and__(self, other):

TypeError: cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index'>

我目前使用的是anaconda python27

最佳答案

试试这个:

X_test = scaler.transform(X_test.astype("float64"))

关于python - 类型错误 : cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index' >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47834516/

相关文章:

python XMLRPC函数错误

Python:统计长度为3的姓氏并将其分配给 "answer"变量

python - 安装 LAMP 会修复 Django/MySQL 配置问题吗?

python - KerasClassifier 无法拟合模型,尽管一切正常

python - 无法使用请求从网页中获取表格内容

python - 当我使用调试版本的 Python 时如何使用 gdb?

python - 使用 Python 和 BeautifulSoup 抓取 Amazon 数据时出错

python - 具有负尺寸的 collide_widget

python - 在运行时向 Django 模型添加属性?

python - 在 Python 中将 CSS ":contains:"与 WebDriver 结合使用