首页 > 解决方案 > 使用 col 名称创建的 Pandas 数据框

问题描述

美好的一天,我正在和熊猫一起工作。我有.xyz数据。想从中创建熊猫。

point_cloud = pd.read_csv("Turning.xyz", delimiter=" ")

point_cloud = point_cloud.iloc[:,1:]
print(point_cloud.values[:5])

我用这种方式阅读。它起作用了,下面是输出。

[[ 5.29851664  2.17091972 -1.5342793 ]
 [ 5.33154001  2.09907462 -1.53531458]
 [ 5.33154001  2.09907462 -1.53531458]
 [ 5.36755701  2.02716027 -1.53738513]
 [ 5.37840056  2.03125554 -1.54049096]]

现在我希望数据框中的 1 col 是“X”,下一个是“Y”,下一个是“Z”。所以我这样做了:

point_cloud = pd.DataFrame(point_cloud.values[:5000], columns={"X","Y","Z"})

我的案子只拿了5000,因为里面有很多点。但是,它没有创建具有正常顺序 XYZ 的数据集,而是使用 YZX 创建的:

          Y         Z         X
0  5.298517  2.170920 -1.534279
1  5.331540  2.099075 -1.535315
2  5.331540  2.099075 -1.535315
3  5.367557  2.027160 -1.537385
4  5.378401  2.031256 -1.540491

这是我第一次看到类似的东西。可能是什么原因以及如何解决?

标签: pythonpandas

解决方案


不要使用 aset因为它是无序的集合。

代替:

columns={"X","Y","Z"}

经过:

columns=["X","Y","Z"]

推荐阅读