我有numpy数组k
的形状(11456,5)。
我正在以这种方式创建pandas数据帧:
submission = pd.DataFrame({"ID":id_test, "Adoption":k[:,0], "Died":k[:,1],"Euthanasia":k[:,2],"Return_to_owner":k[:,3],"Transfer":k[:,4]})
submission.head()
然而ID列现在位于中间,对我来说这看起来很奇怪:
Adoption Died Euthanasia ID Return_to_owner Transfer
0 0.167068 0.005591 0.005023 1 0.813333 0.008985
1 0.610528 0.003572 0.003822 2 0.376651 0.005426
2 0.732384 0.002936 0.003082 3 0.256638 0.004959
我找到了解决方法如何解决这个问题:
cols = ['ID',"Adoption", "Died", "Euthanasia","Return_to_owner", "Transfer"]
submission = submission[cols]
但为什么会这样呢?
答案 0 :(得分:2)
我认为您需要向cols = ['ID',"Adoption", "Died", "Euthanasia","Return_to_owner", "Transfer"]
pd.DataFrame({"ID":id_test, "Adoption":k[:,0],}, columns=cols)
构造函数添加参数列:
columns
因为如果未设置参数{{1}},则会对列进行alfanumeric排序。