Home > OS >  Pandas dataframe creating with col names
Pandas dataframe creating with col names

Time:11-02

Good day, I am working with the pandas. I have .xyz data. Wanted to create pandas from it.

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

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

I read with this way. It worked and following is the output.

[[ 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]]

Now I want that 1 col would be "X" in the dataframe, next one "Y", and next one is "Z". So I did it like that:

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

I took only 5000 for my case, as there are lots of points in it. However it does not created the dataset with normal order X Y Z but it created with Y Z X:

          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

It is the 1st time I saw something like that. What could be the reason and how to solve it?

CodePudding user response:

Don't use a set because it's unordered collection.

Replace:

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

by:

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