【发布时间】:2016-04-06 04:20:23
【问题描述】:
我有 Whole_mat 作为熊猫 df。 corpus_index 作为我想要复制到 New_mat 中的有效行,我只想要列号 1、4 和 7。但顺序应该是 7、1、4。下面是我尝试过的,但我得到 TypeError: unhashable type: '列表'。整个垫子的形状是 Nx10,我想要 New_mat 的 nx3。
New_mat = []
for i in range(len(corpus_index):
index = corpus_index[i]
New_mat.append(Whole_mat[[index], [7,1,4]])
print New_mat
有什么更好的方法可以解决我的问题?
【问题讨论】:
-
可能你应该使用
New_mat.append(Whole_mat.loc[index, [7,1,4]])如果你有7、1、4作为列名 -
这是抛出错误 "'None of [[7, 1, 4]] are in the [columns]'" 。我必须将列名作为字符串给出吗?像 [ "user_id", "phone_no" ] ?
-
是的,你应该这样做。你也可以通过
columns和New_mat.append(Whole_mat.loc[index, Whole_mat.columns[7,1,4]])。 Note 索引从 0 开始。 -
请原谅我提出愚蠢的问题,但这对我来说很容易:/。我收到
too many indices for array同一行New_mat.append(Whole_mat.loc[index, Whole_mat.columns[7,1,4]])