【发布时间】:2017-12-22 14:07:27
【问题描述】:
让我们考虑来自this example的房价数据集。
我将整个数据集存储在 housing 变量中:
housing.shape
(20640, 10)
我也做了一维的OneHotEncoder编码,得到housing_cat_1hot,所以
housing_cat_1hot.toarray().shape
(20640, 5)
我的目标是连接这两个变量并将所有内容存储在一个数据集中。
我试过Join with index tutorial,但问题是第二个矩阵没有任何索引。
如何在 housing 和 housing_cat_1hot 之间进行 JOIN?
>>> left=housing
>>> right=housing_cat_1hot.toarray()
>>> result = left.join(right)
Traceback(最近一次调用最后一次):文件“”,第 1 行,in 结果=left.join(右)文件“/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pandas/core/frame.py”, 第 5293 行,加入 rsuffix=rsuffix, sort=sort) 文件 "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pandas/core/frame.py", 第 5323 行,在 _join_compat can_concat = all(df.index.is_unique for df in frames) 文件“/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pandas/core/frame .py", 第 5323 行,在 can_concat = all(df.index.is_unique for df in frames) AttributeError: 'numpy.ndarray' object has no attribute 'index'
【问题讨论】:
-
好吧,如果你这样做 to_array 它就会变成一个 numpy 数组。 Join 需要一个数据框或一个系列,而不是一个数组。也许
left.join(housing_cat_1hot)就是你所需要的
标签: python pandas join one-hot-encoding